Add login system to comment system in PHP - PHP tutorial

Add login system to comment system in PHP - PHP tutorial
Add login system to comment system in PHP - Learn PHP backend programming. Today we will add a login to our comment section which means we can save user data in each comment instead of it being anonymous.


mmtuts is a YouTube channel that focuses on teaching beginner and advanced courses in various multimedia related skills.
We plan to make tutorials available on programming, video production, animation, graphic design, and on software such as the Adobe Creative Cloud programs.

PHP for beginners is a how to series that teaches the PHP coding language to people who are just starting out learning programming. The course teaches how PHP scripting can be made easy and teaches how to build many apps such as a login system, a comment section, how to upload images, how to create users in a website, and much more. Creating dynamic websites with PHP is easy and should not be seen as otherwise, which is why we want to explain the language in a easy to understand way for beginners.

If you have suggestions on new courses, or specific lessons within existing courses you would like to see, then feel welcome to submit them in the comment section or in a private message. ALL suggestions will be seen, but not all will be replied to since we get quite a few every day.

RRelated Posts

Why I Left My $100,000+ Job at Google

Here’s why I left Google and why I am NOT following my passion! :P Keep in touch on Facebook: https://www.facebook.com/entercsdojo Support me on Patreon: https://www.patreon.com/csdojo My


  • Lillian VanCoonis
    Your code at 25 minutes in is different than what we worked on these last 3 videos. What happened?
  • Reaber Saadi
    I would be great if you provided the source files :) as the comment section tutorials are very lengthy and quite boring no that your boring!
  • Kai Qin
    I want to add an alert box for when login fails any idea how one would do so?
  • himanshu
    yes it worked!!! thanks a lottttttttt love you mate ❤
  • Himanshu Armstrong
    your code doesn't work
  • music boy
    sir your login system is not good
    where is email and where is image system
    plz re build system thise
  • Shohel Rana
    hi bro i have done everthying right until the end but no error for some reason it wont log me in .plz send me the sorce code xxshanexx92@gmail.com
  • tomas lungs
    umm, i didn't see the comment section where , u need to login for the comment section appear, help me plsss, anyone?
  • Albert Garcia
    very nice video sir. job well done. thanks a lot for making tuts for us to learn and more.
  • Duncan Muchiri
    Hi there, I am having this warning : mysqli_num_rows() expects parameter 1 to be mysqli_result, from this line of code
    if(mysqli_num_rows($result) >0)
    any suggestions
  • jack warren
    Hey mmtuts,
    First of all i really love your tutorials have been following from your first html.. i have had no problems up until now i have re watched this video 3 times now and cant figure out what i have done wrong my code looks identical to yours however i am using users rather then user... it wont seem to let me log in.. just keeps spitting back loginfailed.. really hope you or someone can help this really buggin me and i want to move on thanks :).. here is the code

    function getLogin($conn) {
    if (isset($_POST['loginSubmit'])) {
    $uid = $_POST['uid'];
    $pwd = $_POST['pwd'];

    $sql = "SELECT * FROM users WHERE uid='$uid' AND pwd='$pwd'";
    $result = $conn->query($sql);
    if (mysqli_num_rows($results) > 0) {
    if ($row = $result->fetch_assoc()) {
    $_SESSION['id'] = $row['id'];
    header("Location: index.php?loginsuccess");
    } else {
    header("Location: index.php?loginfail");

    function userLogout() {
    if (isset($_POST['logoutSubmit'])) {
    header("Location: index.php");

    for index -

    "<form method='POST' action='".getLogin($conn)."'>
    <input type='text' name='uid'>
    <input type='password' name='pwd'>
    <button type='submit' name='loginSubmit'>Login</button>
    "<form method='POST' action='".userLogout()."'>
    <button type='submit' name='logoutSubmit'>Log Out</button>

    if (isset($_SESSION['id'])) {
    echo "You are logged in!";
    } else{
    echo "You are not logged in!";
  • Joe Fraser
    Is there a way to allow people to individually reply to a comment that you post?
  • Gibbu
    Is there a way to make it say the persons username before the logout button (When they're logged in)??

    I've tried:
    $username = $_SESSION['username'];
    echo $username;

    $username = $_POST['username'];
    echo $username

    And nothing is working. help plz? :'(
  • Moderategamer /
    Hi I've been following your tuts up till this point and thought it would be good at this point to go back and follow your sign up tutorials which I did and everything is working fine. My problem is that when I started to encrypt the password I found that I couldn't merge the login function with the encrypted login function I've tried a few different ways but cant seem to get it working. I'd really appreciate it if you could help me out.
  • sharla ayson
    what if u create a login system separate in your comment system? who u put your user id or user name?
  • Jelmer
    Hi, I used the login system from your earlier episodes. Now you talked about the exit(); function in this video. And I did not use it on the login and logout functions because I made them on the first way in another php document (so it's not a function).
    Now I still have the problem when I comment something and then refresh the page I get a popup notification and if I continue my comment appears once more.

    How do you fix this? I tried to put exit(); after all my header functions. But that didn't work.

    Can someone help me please!
  • Gil Nahum
    I have a general question for you, why do you put the form inside PHP tags and echoing it out instead of keeping it as an html code?
  • Azariah Kerketta
    It won't login me in ! What can be the possible error ?
  • Cent Ci
    Hi. I have been following the tuts and everything was working good up to lesson 48 it won't log me in.
  • Bloofin
    i am getting this error:

    mysql_num_rows() expects parameter 1 to be resource, object given in C:\xampp\htdocs\learning\commentsystem\comments.inc.php on line 93

    I need some ideas to see why I get this error message when I try to login.

    NB. I am at 27:03
  • mb
    This is where I stopped watching your videos. Between the errors that arose with switching paradigms (things you never explain or explore) and the way you jump around instead of building on knowledge (where does the encryption so many videos back fit into the password checking? why is there on encryption when preventing SQL injection?) these videos seem very thrown together and not well thought out.
  • Funky Attitude
    Man! So far so good, but I have a dumb question :(
    Will the signup system that we created previously with login.inc.php where we had 3-4 include php files work with this new type where we actually just have functions for the login ? :S Sorry but I stuck here :(
  • Thabelo Budeli
    when i refresh the page using f5 keyboard button it still gives the resubmit error. even after using the exit();

    help me out here please. ive even tried to delete everything and start afresh but still encounter the same problem
  • Roudy Courser
    Hi. I have followed the tuts throughout and everything works up to this point except it won't log me in. Is it something to do with mixing OOP with procedural? I tried both mysqli_fetch_assoc($result) and $row = $result->fetch_assoc() within the comment.inc file. I've also checked my db, created new users but it won't log me in. Any help would be greatly appreciated.
  • L Tulip
    Hi , I followd your tutorial,but in here,I add a echo,I want to show the username in the front page,but it always alert"undefined index",,I think at the start of the index.php,we had do this "include 'comments.inc.php';",it doesn't include the variable declare?
    "if (isset($_SESSION['id'])) {
    echo "You are logged in!".'<br>';
    echo $_SESSION['id'];
    echo $_POST['uid'];//here,undefined index
    } else {
    echo "You are not logged in!";
  • Jared
    For some very strange reason:
    if (isset($_SESSION['UserID'])) {
    echo "Your are logged in!";

    Does not work.

    I have tested placing a session_start(); in my comment.inc.php file and I receive an error stating that a session has already been started so its ignoring it and yet the isset is not working as expected?

    Gone through, changed and re-checked my code numerous times and cannot find the issue. Even changing it to !isset and swapping the echoes around produces the same problem.

    Using WAMP if that helps in anyway?
  • moreshwar dalvi
    Hi, i have been following your series, i want to add user's fist name and last name from user table to the profile page. I hope you will help me!
  • Pavel Ohrádka
    Nice video, I'm just a bit confused right now. I've watched the whole series so I thought you are gonna use the same site. But in this episode you created another login form (differently), but you already did it in Episode 39-42. Why are you mixing it up? :(
    I'm so confused that I don't even know what is already in the code and what not.
  • Joie Assani
    I expected this answer but let's me tell you something that other people didn't if you would like just to email me and ready what I have to say and then you will make your decisions. Is it okay??
  • Joie Assani
    My email is joieassani12@gmail.com
  • Joie Assani
    Please can we talk privately?
  • Joie Assani
    where do you live ??
  • D Feng
    I created one when you taught us how to create a login system.Do we need to create another login system if we already created one beforehand?
  • Calvin Ng
    Hey, where have you been? Welcome back!
  • Collins Momodu
    Khj Hyundai mh ) ;]]:::""))
  • Cheeky_Tree
    Long time no see c:
  • Rasmus Hedeager
    Awesome, thanks for taking the time to make these videos!