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
  • Pingback: byK – Simple PHP Login Tutorial

  • Pingback: bulldoggie studio » 120 Tips, Tricks, and Tuts from 2009 Worth your Time

  • Pingback: 120 Tips, Tricks, and Tuts from 2009 Worth your Time | Programming Blog

  • Pingback: Neil Skoglund » Blog Archive » 120 Tips, Tricks, and Tuts from 2009 Worth your Time

  • http://www.clippingimages.com Tanjila Jesmeen

    it’s really simple n nice.helpful post.thanks for sharing.

  • Pingback: 120 Tips, Tricks, and Tuts from 2009 Worth your Time | Tutorial51

  • nRadiant

    With the following on the target page that you want to grant access to if user is authorized:

    confirm_Member();

    ?>

    Why would I be seeing

    confirm_Member(); ?>

    in the top left of the target page when I should be redirected to the login page when no session has set?

  • Pingback: 120 Tips, Tricks, and Tuts from 2009 Worth your Time | Synergy Webdesign

  • http://www.drinkthisshow.com/ Glenn

    Awesome prize. Wouldn’t mind getting one!

  • Levi V

    I can’t seem to find the part 2 of this series. Was it ever done?

  • thomas

    hello cest génial j aime beaucoup

  • Emil

    how did you pople do it did you just write it down manually ??.

    where is the for the beginners version????

  • http://xandesigns.com Alex McCabe

    part two would have been absolutely perfect for what i needed. shame it wasnt made =[

  • http://abishekravi.blogspot.com/2010/01/tweaking-phpmyadmin-configuration.html Abishek Ravi

    Dear Jeff

    Thank you for tutorial. initially I had some problems configuring the WAMP server on 8080. I figured out a simple process to do it. I have posted about the same at:

    http://abishekravi.blogspot.com/2010/01/tweaking-phpmyadmin-configuration.html

    Your comments would be very valueable.

  • http://www.modernwarfare2ghost.com/membershipSite/login.php phil

    i have followed the tut to the letter but this comes up?

    http://i46.tinypic.com/2qk1g68.png

  • Rupert

    Sorry I am a noob to OO but I don’t understand why you have structured the validate_user and the verify_Username_and_Pass functions in the way that you have.

    It seems to me that you could have a need of a Mysql class without having a need for any sort of membership functions. Furthermore, having a membership class implies that anything to do with membership should be found there.

    Consequently, it makes more sense, to me at least, to have a validate_user function in Membership and then put all of the logic for validating the user in there, including verifying their username and password. The Mysql class would then only contain basic functions for connecting to the database and so forth and this “rawer” functionality would be called directly from the validate_user function.

    Put it another way, when you want to verify a username and password independently of validating a user?

    Oh well. I am currently creating a Session class for this very purpose so hopefully there aren’t some very good reasons underlying the structure you have chosen. I am sure I will encounter them if there are :).

  • Drizzy

    I followed the Tut exactly, but nothing shows up on my page
    when I test on my computer locally it shows up fine with no errors, but as soon as I put it online and get a blank screen

    http://www.magicbuscompany.com/membership/login.php

    • drizzy

      nevermind I upgrade to php5 and its showing up now, but it keeps giving me a incorrect username and password

  • Drizzy

    I also put in a php info inside to see whats up, but it shows up fine in there

    http://www.magicbuscompany.com/membership/phpinfo.php

    can someone please help me out here I know its probably something really simple I’m not doing.

    Thanks

    • drizzy

      nevermind it was something with my hosting account and the hash password wasn’t typed in correctly

  • PDesignX

    you don’t have to paste your md5 hash to the field in phpmyadmin…

    next to the field there is a select that you can select a mysql function… you can directly write your password and select MD5 as the function and sql query will convert it to md5 has automatically…

  • Edward

    Excellent tutorial for beginners.! I learned a lot.

    When can we see the PART 2?

    Thanks a lot Jeff!

  • Pingback: Useful PHP Components & Tutorials for Everyday Project « UR-Technology

  • http://1deal.site.nfoservers.com/ carl

    having trouble hooking the log in to my website….

    Warning: mysqli::mysqli() [mysqli.mysqli]: (42000/1044): Access denied for user ‘css1deal’@'localhost’ to database ‘team’ in /usr/www/css1deal/public/member/classes/Mysql.php on line 9

    this would be referring to my information in the constants folder which i have correct?

    • http://1deal.site.nfoservers.com/ carl

      i was able to get rid of that error
      but still not able to log in yet

  • Pingback: Implementing User Functionality with PHP and mySQL

  • Pingback: 35 Really Useful PHP Tutorials And Development Techniques | Smashing Buzz

  • maxwolf

    With the following on the target page that you want to grant access to if user is authorized:

    confirm_Member();

    ?>

    Why would I be seeing

    confirm_Member(); ?>

    in the top left of the target page when I should be redirected to the login page when no session has set?

  • Jay

    Thank you for the helpful tutorial. I would also love to see a part 2.

    I also noticed in the screen cast you were having trouble with dreamweaver not placing the closing curly brace at the beginning of a new line. That is kind of a visual queue that you have not closed your preceding parenthesis correctly. Whenever that happens just go add the correct number of closing parenthesis and the closing curly brace will be placed at the beginning of the new line.

  • glolar

    Tutorial is great, but I cannot find anywhere in phpAdmin HOW to create a named database connection. Can anyone offer help on that?

    TIA

  • http://www.sudarshansoft.com kam

    get more details of login on below link

    http://www.sudarshansoft.com/website-login.html

  • Pingback: 30 Useful PHP Tutorials « Web Creation in Cambodia

  • Pingback: ITキヲスク | 2010年6/6~6/12の週間ブックマーク

  • richard

    this was an awesome tutorial i have used it to set up a secure voting site and it works great. however, if i wanted to keep each user from voting more than once how would i go about that? and is it possible to display which username voted? i have two seperate tables one for usernames and one for the vote data. if any other info is needed i will provide it.

  • John

    Fatal error: Class ‘mysqli’ not found in /home/hatewave/public_html/logintest/classes/Mysql.php on line 9

    How can I fix this error?

  • http://rochiyat07.student.ipb.ac.id Rochiyat

    Enjoy….
    Like this..

  • Anon

    Count me in for wanting to see part II. Excellent collection of posts, by the way.

  • Ivo Jenni

    Cool tutorial!!

    How can I get the user ID of the session for the content of the secured pages?

  • kyle

    Just a clarifying question.

    On our websites do we change the names of your “localhost” and “jeff_way” to our server name?
    I am a bit confused about this and a little explanation would be greatly appreciated.

    thanks

    • http://www.xreals.com Xreals

      Hey Kyle to answer your question, yes, you do have to change those things such as localhost, jeff_way, and the password…..that is if you are to put it on your own site on a webhost like godaddy or limewebs. However,if you are just developing on your local system then you must keep localhost, and whatever username and password you may have for your database, if you have just downloaded WampServer Xaamp, or EasyPHP your database manager would be PhpMyAdmin, if you have not messed with the account prefferences of PhpMyAdmin it would be to the default as Localhost root and the password value will be blank just leave the qoutes empty “”. Any questions just email me at sxmenergy@gmail.com

  • Kmichaud

    Hi, i just follow the tutorial and want to thank you.

    But there is one problem. When i enter my username and password, it always return me that they are incorrect. I put the good info, the same that i have in my DB.

    Can someone help me make this work

    • Jorre

      I got the same problem.
      Anyone can help us?

      • http://kimhallberg.tumblr.com/ Kim

        Are you guys sure that everything checks out? Mysql connections, all hashing and everything? Do a quick error searching, it’s probably something easy. :)

  • Reconix

    Awesome post, thanks a lot !!

    Very easy to follow and understand, plus you’re very easy to listen to, that makes a big plus in these video tutorials.

    I’m guessing you ain’t got the time for part 2 just yet or possibly forgot about it. It would however be really interesting to see what you come up with next time.

    Never the less,keep up the good work !

  • http://kimhallberg.tumblr.com/ Kim

    Jeffrey, when is the second tutorial coming up? Don’t find it if you’ve uploaded it, know that it was along time ago when you did the tutorial, but, I thing I speak for most of us when I say that I really want that second tutorial.

  • Amit

    Thank you very much! Very helpful!

  • Philip

    I havent inported the source into my webserver, and chance info in constants.php but i log in into myphpadmin and create a user, but if i try to login then says, my username and password was incorrekt, whats wrong???

  • Dariusz

    when will be the second part? Can’t wait to read it.

  • http://ll444310 onenji

    cool

  • http://lowsand.com nico

    héy,
    thanks a lot for this!
    excellent tutorial, I never thought something like this
    could be so perfectly clear to me in such a small amount of time ;))

    thxthxthx:::
    nico

  • Michael

    Awesome Tutorial :), how would I go about making admin area?

  • Raymondim

    I was just wondering if the part II was ever posted for this?

  • http://www.irunmanhq.com James

    Awesome Tutorial, suggestion for part 2…could there be a way to set up different user levels?? Meaning a group of user can see page A, then another group of user’s with higher access is allowed to see page A and B.

    thnx! =)

    • http://thetechguia.wordpress.com Guilherme

      That would be really interesting! I also would like to know. By the way, great tutorial Jeff! Cheers!

    • ITS_GOOD

      Its a good tutorial.

  • pearl

    this is very useful. thanks!

  • Pingback: 智慧本 » 40个宝贵的PHP教程和资源

  • http://www.jhproplastering.co.uk/login Genesis

    Hi, Nice work but I don’t seem to get that work. Keep showing me the

    Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or ‘}’ in /classes/Mysql.php on line 6

    The only thing I have changed in the Mysql.php is added prefix to the database table so that match with the table I have created.

    on line 6 is the
    private $conn;

    If I remove this code then it will give me the logon screen, then of course it cannot call the conn object when it hit line 20.
    if($stmt = $this->conn->prepare($query)) {

    I’m just a newbie and wonder if you can tell me what went wrong. Many thanks for the tutorial

    Genesis

    • Dan

      Was there ever a fix to this error on line 6? I am getting the same thing.