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
  • http://lowsand.com nico

    hey—thanks a lot for this tut!! was really easy to learn & great to use!
    …I second that question of Ivo ~ any possibility of getting an ID out.. or
    redirect users with different passwords to different places/pages ?

    (also just a short answer, wheter this customization would be “possible” at all with
    this tutorial as a base, would be great & time saving;)

    thx
    ~ nico

  • Techeese

    Hey jeffrey, cool tutorial but where is your part 2,
    I want to know about, how you’d put the cookies to expire in this log in form

    I hope you read this…

    • Techeese

      Jeffrey, how do i put an expiration on this log in form?

  • http://www.myhipstyle.com sengyi

    Hi Dan and all,

    Currently i coding a page and i face with sql error.

    $Account_ID = 1
    $delete_id = 21,23

    Using the function below, it only delete 1 row from my database

    function delete_transaction($Account_ID,$delete_id)
    {
    $query = “delete from transaction where Account_ID = ? and Transaction_ID in (?)”;

    if($stmt = $this->conn->prepare($query)){
    $stmt->bind_param(‘is’,$Account_ID, $delete_id);
    $stmt->execute();
    $stmt->close();
    }else return ‘fail’;
    }

    if i code it this way it will remove 2 row

    function delete_transaction($Account_ID,$delete_id)
    {
    $query = “delete from transaction where Account_ID = ? and Transaction_ID in (“.$delete_id.”)”;

    if($stmt = $this->conn->prepare($query)){
    $stmt->bind_param(‘i’,$Account_ID);
    $stmt->execute();
    $stmt->close();
    }else return ‘fail’;
    }

    anyone?

  • Rodrigo

    Hi,

    I have to say that is a very good toturial, i was visiting the site and found exatly what i need.

    I have one question.

    In Membership.php there are not one mistake? In line 22 the command “if” don’t should have ” { } ” ?

    You have this:


    if(isset($_COOKIE[session_name()]))
    setcookie(session_name(), ”, time() – 1000);
    session_destroy();

    Don’t should be this:


    if(isset($_COOKIE[session_name()])) {
    setcookie(session_name(), ”, time() – 1000);
    session_destroy();
    }

    Sorry for my English….

    Regards, Rodrigo from Portugal

  • Pingback: 15+ Useful PHP Components, Techniques and Tutorials « pinoygsm.info

  • http://statiquegraphics.co.nr EmilyAnne

    Hey. This is probably a very amateur question but how do you get to localhost?

  • Hari

    hi I m just learning web designing however I designed a website and would like to add login page on my site could you please help me .
    I tried your codes however I have not succeeded.
    so do i have to change any thing in membeship codes ..

    Hari
    Bangalore!

  • http://www.quali-x.de Wiyono

    Thank you
    Work perfect…

    Heheheeee…
    (◑‿◐)

  • http://moderntecno.com Kevin

    Fatal error: Call to undefined method mysql::verify_Username_and_Password() in /home/content/70/4915070/html/classes/membership.php on line 9

    Getting this error but not sure what’s wrong
    help please

  • Daniel Miller

    I cannot make it work it just redirects me towards error incorrect username etc.. when i try to log in

  • Pingback: Crear un sencillo sistema de autenticacion [PHP] | MadHacking.com - Ethical Hacking

  • Jack

    @Jeff

    This is exactly the kind of thing I was looking for! It works perfectly for my site.

    I could REALLY use the functionality of Part 2! Especially users being able to register and edit their own passwords. I realize this tutorial was written a little over 2 years ago but I would very much appreciate seeing part 2. If nothing else, could you at least confirm or deny Part 2 is being made?

    Thanks,

  • Alexandra

    A great tutorial – one question anyone knows how to create this DB_USER in case of the screencast
    it was jeff_way and the password. Where create it??
    Thanks to anyones who answers me.

  • ante

    Warning: mysqli::mysqli() [mysqli.mysqli]: (28000/1045): Access denied for user ‘username’@'localhost’ (using password: YES) in C:\xampp\htdocs\membershipSite\classes\Mysql.php on line 9

    Warning: mysqli::prepare() [mysqli.prepare]: Couldn’t fetch mysqli in C:\xampp\htdocs\membershipSite\classes\Mysql.php on line 20

    Here i got a problem i cant solve… plz help me what is wrong

    • manfm

      i had the same issue.

      it turned out to be that i spelled construct wrong “function __construct() {“

  • Stevon

    Hey great tutorial, but how do I get it to show the the users name anywhere on the page on the index.php? from the session, thanks…

  • MoveduemOvece

    I0P0317

  • stefan

    Nice tutorial. But now how i can create a register script for this ??? Please help me

  • manfm

    its unfortunate that after two years later, there still is not a part 2

  • Dave Bigman

    Part 2? I can’t wait for part 3!

  • http://nus.edu.sg BorrorNataliaju
  • Given

    Before this video, I knew next to nothing about PHP coding as well as using the classes and connecting to a database… After close to an hour of being glued to my screen, I know A LOT more than I’ve ever known. Really appreciate you taking the time to explain all this, extremely helpful. Thank you!!! :-)

  • Marcinkonys

    I just had to say “Thank You, Jeff!” I’ve been looking for a login tutorial for a while now, and this worked perfectly! Really appreciate the effort you put into making tutorials and screencasts :)

    Martin

  • Alex

    I’m getting the same access denied problem as others:

    Warning: mysqli::mysqli() [mysqli.mysqli]: (28000/1045): Access denied for user ‘username’@’localhost’ (using password: YES) in C:\xampp\htdocs\membershipSite\classes\Mysql.php on line 9
    Warning: mysqli::prepare() [mysqli.prepare]: Couldn’t fetch mysqli in C:\xampp\htdocs\membershipSite\classes\Mysql.php on line 20

    My web hosting service uses Cpanel X. I followed the tutorial and created a new database and user in the phpMyAdmin. I then went into the MySQL Databases and added a new user and added that user to the membership database I created and gave that user all privileges (I don’t know why, but I thought maybe I needed to create a user and password to put into the constants.php).

    I have tried using blanks for the DB_USER and DB_Password, I tried using the login information for my CPanel, and I tried using the login info I created in the MySQL panel and I continue to get the same error.

    Anyone know how to fix this error or set it up correctly using CPanel? Also for the DB_Server do I use the website IP address?

    • http://www.neilrpearce.co.uk Neil Pearce

      That error shows you are using localhost(xampp) still and not a web host. If your using localhost(xampp, wamp, mamp) then dont use a password, just leave it blank? That might work?

      BUT, i am having difficulties logging in myself on a web host and it keeps saying “Please enter a correct username and password”? Works find locally but not remotely.

  • keith

    hey,

    im building a simple website with an Ai bot in it and i have used this tut to make the login and it worked when i used wamp. but my ai bot wouldn’t, but the ai bot worked on server2go but now my login page keeps saying that it cant connect to the database.
    Even though i have changed the constants.php file and it works, the reason i know that it works is cause i have written my own register.php file and it writes to that database through mysql.php and that is connected to constants.php.

    i think its because with wamp it looks for the password and because there is one it works fine,but in server2go there is no password linked to the server and i think that is why it can not connect.

    the error is:

    Warning: mysqli::mysqli() [mysqli.mysqli]: (HY000/2003): Can’t connect to MySQL server on ‘localhost’ (10061) in C:\Users\keith\Desktop\My Project\AI_server2go\htdocs\membershipSite\classes\Mysql.php on line 9

    Warning: mysqli::prepare() [mysqli.prepare]: Couldn’t fetch mysqli in C:\Users\keith\Desktop\My Project\AI_server2go\htdocs\membershipSite\classes\Mysql.php on line 20

    any help would be great

    thanks

  • Alessandro

    Is a part 2 ever coming out? i need it badly, this video was the best way to learn this stuff

  • http://people.pwf.cam.ac.uk/mm821/ Moreno

    This is an excellent tutorial, Jeffrey! Thanks very much!

  • http://www.rudyrosciglione.com rudy

    Hi

  • http://www.rudyrosciglione.com Rudy

    Hi Jeffrey i’ve problem with your Mysql.php file

    ” Warning: mysqli::mysqli() [mysqli.mysqli]: (42000/1049): Unknown database ‘membership’ in ../privateaccess/classes/Mysql.php on line 9

    Warning: mysqli::prepare() [mysqli.prepare]: Couldn’t fetch mysqli in …privateaccess/classes/Mysql.php on line 20 ”

    Can you help me ?

    • http://trauma-gaming.org Dartsson

      @RUDY

      You using wrong database name

    • Tyler

      SIMILAR PROBLEM:

      I cannot figure out why I keep getting the same error:

      Warning: mysqli::mysqli() [mysqli.mysqli]: (28000/1045): Access denied for user ‘a9010378_RBAdmin’@’localhost’ (using password: YES) in /home/a9010378/public_html/LOGIN/classes/Mysql.php on line 9
      I am using 000webhost.com with myPHPAdmin.

      Here is the link: http://www.reviewbubble.site50.net/LOGIN/login.php

      Username: username
      Password: password

      Please help!

  • Andrew

    This tutorial is so much cooler because you sound like Jesse Eisenberg from The Social Network movie. makes me want to code even more!

  • daniel

    hi, the video is down, could u upload it again pl0x? :_P

  • devansh

    i am getting this error help

    Warning: mysql_connect() [function.mysql-connect]: Access denied for user ‘devansh_login’@'int.web4.vital.x10hosting.com’ (using password: YES) in /home/devansh/public_html/php/login.php on line 8
    Couldn’t connect

  • Ernesto

    This was a truly awesome tut, I really appreciate it, and I really want to see part 2, it seems as though you’ve forgotten to make it, but Im sure its for a good reason, anyway I hope to see part 2 soon, great job!!!!!

  • Tyler

    I cannot figure out why I keep getting the same error:

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

    I am using 000webhost.com with myPHPAdmin.

    Here is the link: http://www.reviewbubble.site50.net/LOGIN/login.php

    Username: username
    Password: password

    Please help!

  • Imran

    Hi,

    Thank you for this great tutorial and source code.
    I am new to php. Can someone tell me how secure this is please? Would you advice using it on a production website?

    Also if i am using SSL, does the security matter as SSL handles the encryption?

    Thank you.

  • kevin

    It works fine on when I use wamp on my computer but when i tried to put it on a server it didn’t work correctly. I don’y know what i’m missing. i change the mysql.php and when I go the the login.php nothing shows up! if i remove the private $conn it will show the login css but enter a un and pw it goes white…

  • Mike

    greate tuttoria. thanks!

    a few notes for newbies like myself, to get things working easier…

    know your mysql user name and password… if you’ve never set it, do this in a mysql consol:
    msql> set password for ‘root’@'localhost’= PASSWORD(‘mypasswd);

    ….. not sure about the syntax, but you can check that on the web at an official site about mysql….

    then in your constants file:

    define(‘DB_SERVER’, ‘localhost’);
    define(‘DB_USER’, ‘root’);
    define(‘DB_PASSWORD’, ‘mypasswd’);
    define(‘DB_NAME’, ‘membership’);

    one other thing i did to make life easier, i didn’t use md5 for the password encryption… just a normal password… i’m still learning, so it just made things easier.

  • http://www.podprysznic24.pl Assurdenuttv
  • Devpriya Shivani

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

  • JSingh

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

  • David

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

  • Jan Akerman

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

  • Taylor

    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.

  • Sam

    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

  • Paul

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

  • ryan

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

  • http://mphelps.x10.mx 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?

    • http://mjphelps.x10.mx Michael Phelps

      I got it… Forgot to close the tag in the login.php

  • Kimi

    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

  • sapdavi

    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?

  • abi-sapdavi

    hello login bye

  • Biftinsissodo

    hello this is me

    ssss