Get $500+ of the best After Effects files, video templates and music for only $20!
How to Build a Login System for a Simple Website
videos

How to Build a Login System for a Simple Website

In today’s video tutorial, we’ll be building a login system with PHP and MYSQL. Many, many features will be covered; including MySqli, Prepared Statements, jQuery, Classes, Sessions, Cookies, etc. I bit off more than I could chew for today’s screencast. So, I’ll be creating a Part 2 in the next couple of weeks in order to improve our system even further.

building a login system
building a login system
building a login system

Finished! ….For Now

We’ve managed to get an enormous amount of work done in about forty-five minutes; but there’s much room for improvement – specifically when it comes to optimizing our class files for reuse. Before I let you go, let’s take a quick look at what we’d like to accomplish in Part 2.

What We Need to Accomplish in a Future Tutorial

  • Set an expiration date after creating a session – so that the user is automatically logged off after X minutes.
  • Allow users to sign up and edit their passwords.
  • Update our database to contain “secret question/answer” questions for username/password retrieval.
  • Refactor our class files. As I mentioned in the video tut, there are a few procedures that we should remove from our methods in order to promote “best practices”, and increase reusability as much as possible.
  • Subscribe to the NETTUTS RSS Feed for more daily web development tuts and articles.


Add Comment

Discussion 373 Comments

Comment Page 7 of 7 1 ... 5 6 7
  1. Devpriya Shivani says:

    i’ve made my website how can i access it?

  2. JSingh says:

    dude its been way over 2 years now…. where’s part 2 at?…

  3. David says:

    When Dreamweaver put your cursor that far indented, that was its way of telling you that you hadn’t closed all of the parentheses.

  4. Jan Akerman says:

    I’d love to see a second part to this!

  5. Taylor says:

    Jeff,

    This tutorial is great. I am really happy with this setup, but want to add more to it and would like part 2 please. Please post part 2 soon.

  6. Sam says:

    Hi! Thank you so much for this awesome tutorial, it’s very helpful. When I run it on my local computer, it works fine. When I try uploading it to a web host (iPage) though, PHP errors out. Here are the errors:

    Warning: session_start() [function.session-start]: Cannot send session cookie – headers already sent by (output started at /*/*/*/*/ipg.*/rovweb/includes/header.html:15) in /*/*/*/*/ipg.*/rovweb/login.php on line 2

    Warning: session_start() [function.session-start]: Cannot send session cache limiter – headers already sent (output started at /*/*/*/*/ipg.*/rovweb/includes/header.html:15) in /*/*/*/*/ipg.*/rovweb/login.php on line 2

    Warning: mysqli::mysqli() [mysqli.mysqli]: (HY000/2002): Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2) in /*/*/*/*/ipg.*/*/classes/mysql.php on line 9

    Warning: mysqli::prepare() [mysqli.prepare]: Couldn’t fetch mysqli in /*/*/*/*/ipg.*/rovweb/classes/mysql.php on line 20

    I obscured some of the names with * fyi. Thank you, Sam

  7. Paul says:

    hi i was wondering what other function i could use instead of mysqli as my host has turned it off

  8. ryan says:

    When someone tried to log into my website then couldn’t log in why is that? How can I fix it?

  9. For those of you using cPanel Hosting, When you create the database and user (under MySQL Databases) Make sure you give the user permissions on the database. That will probably solve any connection errors. Also make sure you are adding the user and database name as xxx_name because cPanel auto-appends your cPanel username onto the front of the names.. so if your cPanel login name is mike and you have a DB and user named mike as well, the connection string will need to look like this:

    define (‘DB_SERVER’ , ‘localhost’);
    define (‘DB_USER’, ‘mike_mike’);
    define (‘DB_PASSWORD’, ‘YOURPASSWORD’);
    define (‘DB_NAME’, ‘mike_mike’);

    On another note, and this is probably something for the JS Folks.. when I add the JavaScript into the code at the end of the tutorial, reloading the page gives me a beautiful blank page. Almost like a 500 error, but I can’t find any error message anywhere.

    Ideas?

  10. Kimi says:

    Hey,
    can you told me what’s wrong? it gives me errors

    Warning: mysqli::mysqli() [mysqli.mysqli]: (28000/1045): Access denied for user ‘username’@'localhost’ (using password: YES) in /home/gteksi/public_html/gaming/classes/Mysql.php on line 9

    Warning: mysqli::prepare() [mysqli.prepare]: Couldn’t fetch mysqli in /home/gteksi/public_html/gaming/classes/Mysql.php on line 20

    and i’m new…
    thank you!
    Kimi

  11. sapdavi says:

    I agree with michael phelps For those of you using cPanel Hosting, When you create the database and user (under MySQL Databases) Make sure you give the user permissions on the database. That will probably solve any connection errors. Also make sure you are adding the user and database name as xxx_name because cPanel auto-appends your cPanel username onto the front of the names.. so if your cPanel login name is mike and you have a DB and user named mike as well, the connection string will need to look like this:

    define (‘DB_SERVER’ , ‘localhost’);
    define (‘DB_USER’, ‘mike_mike’);
    define (‘DB_PASSWORD’, ‘YOURPASSWORD’);
    define (‘DB_NAME’, ‘mike_mike’);

    On another note, and this is probably something for the JS Folks.. when I add the JavaScript into the code at the end of the tutorial, reloading the page gives me a beautiful blank page. Almost like a 500 error, but I can’t find any error message anywhere.

    Ideas?

  12. abi-sapdavi says:

    hello login bye

  13. Biftinsissodo says:

    hello this is me

    ssss

  14. Pablo says:

    Hi Jeffery, totally amazing tutorial…I’m a beginner and that was better than my in-class sessions.Great work. Wish you were local so we could work on my startup!

    Thank you very much bud!

  15. Mohammed says:

    Thank you, i was looking for php [ session ] tutorial, and now i learned the basic.
    thank you and god bless you

  16. Dave says:

    Awesome, but where is part 2, would be amazing to see where you take it

  17. Qaysar says:

    Hi,

    Can you please advise when part 2 will be done??

    Seems that this tutorial has been completely been forgotten about.

    Part 2 would be awesome.

  18. iPage says:

    I’m enjoying the tutorial. When’s part II coming out?.. It will be cool..Thanks for sharing!

  19. AjayP says:

    Amazingly helpful . . . sad it seems that part 2 is not to arrive . . .

  20. Rsam91 says:

    Where is part 2?

  21. nintendofan345 says:

    Using this I get “Call to a member function prepare() on a non-object in *location* on line 20″ errors when I submit any username/pass combos.

  22. very nice..i just used this php for my new website…looking forward more tutorials..

  23. Dan says:

    Hi,
    I followed the tutorial to the letter by i keep getting the following error:

    Fatal error: Call to a member function prepare() on a non-object in C:\xampp\htdocs\membership\Mysql.php on line 19.

    Here’s my code from the Mysql.php:

    conn = new mysqli(DB_SERVER, DB_USER, DB_PASS, DB_NAME ) or
    die(‘There was a problem connecting to the database.’);
    }

    function verify_Username_and_Pass($un, $pwd)
    {

    $query = “SELECT * FROM users WHERE username = ? AND password = ? LIMIT 1″;

    if($stmt= $this->conn->prepare($query))
    {
    $stmt->bind_param(‘ss’, $un, $pwd);
    $stmt->execute();

    if($stmt->fetch())
    {
    $stmt->close();
    return true;
    }
    }
    }

    }//end of class Mysql

    ?>

    I am new to using prepared statements so am not sure where the error is, please help.

  24. cameron farrell says:

    im pretty sure i followed all of the videos instructions but when i go to the login page and login in it doesnt do any thing. i think it has something to do with the database or the database connecting to the login

  25. Scott says:

    Will a second part of this come out?

  26. Chris says:

    verify_Username_and_Pass($un, nd5($pwd));
    if($ensure_credentials)
    $_SESSION['status'] = ‘authorized’;
    header(“location: index.php”);
    } else return “Please enter a correct username and password”;
    }
    }

    When I type all that in and try to go to http://localhost/files/login-register/login.php I get an error message that says “Parse error: syntax error, unexpected T_ELSE in C:\xampp\htdocs\files\login-register\classes\membership.php on line 12″ but I can’t find what that error is, or whats wrong.

  27. danema says:

    I added the following to login.php since logged-in users should never see the login-window till their logged out:

    if($_SESSION['status'] ==’authorized’) {
    header(“location: index.php”);
    }

  28. I RESURCH TO VIDEO HISTRY.

  29. Thank you SO much for creating this video. I followed it from start to finish and was able to get everything working the same as your examples. I am still a little in the dark with the PHP side of things, but I have been brushing up on my JS skills on codecademy.com which really helped a lot. Now to find another tutorial on how to allow people to register for their own account using this as an example! Anyone know any good links??

    Thanks again!!!!

  30. dennis says:

    please is there anyone who can explain how i can create a yahoo sign in page,and the result page where all important informations will be sent to.please i will be very much pleased if i can get an answer

  31. Mike.H says:

    Hello, I was wondering if part 2 is coming out anytime at all. Thanks

  32. Mick says:

    I press login and nothing happens :(

  33. calvin david says:

    the bg.png is missing from the downloaded source
    Regards

Comment Page 7 of 7 1 ... 5 6 7

Add a Comment

To add a code snippet to your comment, please wrap your code like so: <pre name="code" class="html">YOUR CODE</pre>. You can replace the class name with "js," "css," "sql," or "php." If there are any "<" or ">" within your code, please search and replace them with: &lt; and &gt; respectively.