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.


Tags: Videos
Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • zoe

    The first site that it shows to go on: localhost.com does not open for me. i really need to make a login server but i do not know how to start the database if the site is not working

  • Guest

    Hi, Can I get the Part 2 of this video link?

  • Justin

    Hi Jeffery, first of all this video is awesome and much appreciated!

    Does
    anyone else have a problem connecting to their MySQL database, no
    matter what i do i cannot connect to it i keep getting our, “please
    enter a correct username and password”

    Thanks,

    • PhilD

      It worked for me the first time, but now I get the same message as you even though the user exists!

  • Justin

    EDIT: Removing “md5″ fixed my problem, looking into a different solution.

    PS: Please revamp this project, Part 2 would make many people happy!

  • trinite

    Very nice tutorial, I’ve implemented this into my project! I have one question though, how can I echo/print the username? I’m still a beginner with PHP and I can’t get it working.

    This doesn’t seem to be working:
    echo $_SESSION['username'];
    neither does
    echo $username

    Thanks in advance!

    • Kurt

      hey, I might not be the one who wrote this tut but have you tried storing the session in a variable and echoing it out later? You could try

      $username = “$_SESSION['username'];

      and then use that variable in an echo statement like so

      echo $username;

      See if that would settle the problem. If not then lol who could blame me for trying to help

  • Guest

    wheres part 2?

  • Alex Mohr

    I must have done something wrong, but google chrome is just showing the source code for index.php rather than the actual page.. what do I do?

  • V!gny

    jeff you ar the Best !

  • http://www.facebook.com/zaid.sauor Zaid Sauor

    very nice work but I have a question

    mistakenly i del mysql from my web server (datebass + table )and i dont have a backup for it . I trid to setup new one but it never worked with my website is there somehow that I can find out my sitting to the databass throw my php pages ?

  • Luke

    I got this error, but I don’t know what to change

    Warning: require_once(includes/constants.php): failed to open stream: No such file or directory in /home/lukecobu/public_html/membershipSite/classes/Mysql.php on line 3 Fatal error: require_once(): Failed opening required ‘includes/constants.php’ (include_path=’.:/usr/share/pear:/usr/share/php’) in /home/lukecobu/public_html/membershipSite/classes/Mysql.php on line 3

    • Kurt

      Did you create the include file? From what I see, it seems like you didn’t create the ‘constants.php’ file or maybe it’s in a different directory from your ‘includes’ folder. Try creating that file or if it’s already created, move it to your includes folder.

  • draculaz

    This is cool, thank you very much….

  • alex

    i receive this error ..

    Warning: mysqli::mysqli(): (HY000/1045): Access denied for user ‘username’@'localhost’ (using password: YES) in C:xampphtdocsmembershipSiteclassesMysql.php on line 9

    Warning: mysqli::prepare(): Couldn’t fetch mysqli in C:xampphtdocsmembershipSiteclassesMysql.php on line 20

    can you helpt me ?

    • Horrible Boss

      Did you try editing the constants.php? This is where the information about your database is hidden. Maybe if you try subsituting the DB_USER, DB_PASSWORD, and DB_NAME with your database information it could work.

    • Ivan

      It’s because your username,pass are probably wrong.. check Constants where you defined them..

    • werd

      Yeah I fixed that by changing db-user and db-password to the correct mysql username and password.

      define(‘DB_USER’, ‘yourmysqlusername’);

      define(‘DB_PASSWORD’, ‘yourmysqlpassword’);

  • alan

    aaa

  • kokey

    uuu

  • TheNew

    Hi! Im new and i want some clarification for this thing “bllaablaa…

    This doesn’t seem to be working:
    echo $_SESSION['username'];
    neither does
    echo $username

    ….blaa

    Kurt trinite • a day ago

    hey, I might not be the one who wrote this tut but have you tried storing the session in a variable and echoing it out later? You could try

    $username = “$_SESSION['username'];

    and then use that variable in an echo statement like so

    echo $username;

    See if that would settle the problem. If not then lol who could blame me for trying to help”

    So, can you give me full code. P.S. Im not good with english, I’m from finland.

  • Wil

    Where is part 2?

    • Noah Wetjen

      Part 2 has turned into an Premium Tutorial and is on Tuts+ Premium

      • shannon

        where is the tutorial, can you post a link? I’m not able to find it, even on tutsplus

  • houston34

    Is this a good tutorial for a newbie to databases and php. I am trying to add a login and password to my site.

  • krabbs

    that is greate! im a total noob yet however your tutorial is a perfect first step. i watched this 4 times and now im able to do next stepts on my own…thank you so much your way to teach is awesome!

    i would love to see a continue of this and i will also stick on this blog to learn more :D

  • Phil D.

    Well, I followed the instruction and it worked except for one tiny error. Then I checked my files against yours, made a few alterations, and now it doesn’t work!!!! Now I have to go through it all again to find out why. Thanks, it did work,,,,,Oh, and the connection via Facebook seems to be a bit off too. Your download files are different to your Video files, which seemed to be ok!

  • Loginto

    Nice tutorial on this. Still messing with PHP, but I think I have it going now based on your feedback going on at http://www.loginto.org for the login page. Anyway, thank again Jeffrey.

  • Nic

    what is the default username or password or how can i create new users, do help me!

  • richard the repair man

    We need Part 2 plz this will help a lot of people.
    I need to know how to add a register function to this plz.:D

  • Georgi

    Thank you man! That is what I need it and now I understand how it works!
    It would be cool if you make register system too. :)

  • anonym

    hello, thanks for tutorial

  • Andrew

    great tutorial, however, I can’t seem to get this to work.

    I have copied everything as directed completely and checked it 1000 times.

    Whenever I test the form, the login button does not do anything.

    I ran tests on it by using var_dump($_POST) but it also just shows an empty array.

    It seems as though the form doesn’t post or something.. so frustrating

  • Steini

    Hello,

    The issue people after implementing the “Confirm_Member()” is simply this


    if($ensure_credentials) {
    $_SESSION['status'] == 'authorized';
    header("location: index.php");

    It is so easy to do this mistake, at least this is my mistake which caused the login never to goto index just kept booting me to login. The code should be this

    if($ensure_credentials) {
    $_SESSION['status'] = 'authorized';
    header("location: index.php");

    I changed this and only this in the "ensure_credentials" and that got my login to goto index. I searched and searched and no one had really given up this information.

  • juli

    part 2 coming soon???????

  • Matt Cromwell

    This is awesome. Just exactly what I needed in order to provide a branded entry page for clients to view their beta sites. Yes, it would be great to add some reusability to this, because it can be extremelt useful and portable across domains.

  • enconc jk

    good tutorial. keep it up guy!!

  • Sayhoun

    I though I share some nice css, I sometimes use to debug

    /* Tags Hover */

    * :hover { outline: 4px dotted blue }
    * * :hover { outline: 4px dotted red }
    * * * :hover { outline: 4px dotted green }
    * * * * :hover { outline: 4px dotted orange }
    * * * * * :hover { outline: 2px solid darkmagenta }
    * * * * * * :hover { outline: 2px solid darkviolet }
    * * * * * * * :hover { outline: 2px solid indigo }
    * * * * * * * * :hover { outline: 2px solid darkblue }

  • Hugo

    I am always getting a error : Fatal error: Class ‘mysql’ not found in (mebership.php) on line 8

    Does anyone know a solution?