How to Build a Login System for a Simple Websitevideos

How to Build a Login System for a Simple Website

DiggThis

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.


Related Posts

Add Comment

Discussion 266 Comments

Comment Page 6 of 6 1 ... 4 5 6
  1. Istvan says:

    Thanks … it was easy to learn … and put in practice

    I’m checking new nettuts from you …

  2. Jason A. says:

    Love the this tutorial. Still waiting for part 2, though. Still going to happen??

  3. Moneyxl says:

    Is it possible to download the screencast itself?

  4. Del says:

    Hi,

    I’m gettin an error saying:

    “Fatal error: Call to a member function prepare() on a non-object in C:\wamp\www\membershipSite\classes\Mysql.php on line 20″

    This happens whether I use the correct username/password or not. I went to line 20:
    if ($stmt = $this->conn->prepare($query)) {

    but don’t see what is wrong. Any ideas?

  5. Adrian says:

    if the user ignores location headers, they will not have to log in to see the secret launch codes.

  6. saad says:

    Thanks for every thing.

  7. mark says:

    i’m looking forward to your future tutorial

    thnx jeff. you rock!

  8. gway says:

    nice tut!
    i was wondering what is the method to hash a password ?

  9. John says:

    Die() function is not working. It’s not dying.

    I simply don’t have database installed even though i when i try with existing database and WRONG password. It’s not dying.

    It just shows error at the top of the page. I just want to make it die().

  10. D'Arcy says:

    Do Prepared Statements return differently with UPDATE instead of SELECT.
    I’ve been using this tutorial to understand Prepared Statements and I have them working fine, except my UPDATE queries return to the previous class false, even if they work just fine.
    Hope that makes sense.

  11. puur says:

    Hii… this video stops in minute 26.. i whats up there ???

  12. SAT says:

    Excellent !! Very precise and we all forgive you for your mistakes during the video ^^

    I love your tutorial !
    Thanks again :)

  13. Bre says:

    Awesome tut – Just what I was looking for.

    Trying to upload the working files to my server that has php myadmin built in.
    I’m pretty sure Ive changed the “constants” file correctly with my user name, password etc but when I login to the login.php page it keeps saying that I have an invalid username/ password although it is one I have entered into php myadmin.

    Any ideas?

    (un: ‘test’ | ps: ‘12345′

  14. Awofe says:

    Hi Jeff!

    Where can I get part 2 of this tutorial?

  15. Lucas says:

    What should I use instead of this:

    conn = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, 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;
    }
    }

    }
    }

    If I dont have MYSQLI module installed?

  16. Sebastian says:

    Hey when will part to be posted? BTW great tuts only reason i got a plus account is to watch the content you post

    thanks

  17. Shane says:

    Hey Thanks! I enjoyed that, and learned a bunch. I’ll be going through your other stuff for sure.

  18. Michael says:

    Part 2 would be great.

  19. Hey everybody, how can i get the details for the user currently logged in?
    As i am building an application form, which inserts to mysql db.

    Any ideas?

  20. Patel says:

    Great tutorial! Thanks a lot to make it very easy and understandable. I believe 2nd part of tutorial is in demand! cant wait…..

  21. @Jeff – Is part 2 on the way soon?

  22. stefn says:

    Thank you for this tutorial, I got it working pretty close to my needs.

    However, how would I go about redirecting the visitor to the page he was *trying* to reach before getting stopped by the login.php page, after he has sucsessfully logged in?

  23. Milo says:

    I still have a problem, both in my own and the downloaded tutorial version of Mysql.php.

    I get this error:

    Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or ‘}’ in /nfs/c05/h04/mnt/77028/domains/silktonguegamblers.com/html/classes/Mysql.php on line 6

    As I don’t know any php, I have no idea how to solve it. Does anyone know why this error comes up? My server is uding PHP5.

    • Milo says:

      Nevermind. It was a regular problem with (mt) servers that it automatically reverts back to PHP4. I had to create a line in the .htaccess file to instruct it to stick to PHP5.

  24. Louise says:

    Great tut!
    This was exactly what i was looking for.
    I have a few questions though,
    1. Could you list all programs used in this tut?
    2. And also, can you use MS Access instead of phpMyAdmin?

    Thanks!

  25. Jeffeaux says:

    I too would love to see the Part 2 followup to this excellent screencast

  26. James says:

    Hi Folks,

    I am going though this php course and unfortunately I have an instructor that does not know how to instruct and does not help in any way. I have duplicated your login script verbatim and for some reason I cannot seem to get it to work. I am using XAMPP on a Mac and would like to know if this would cause any problems as to why I cannot seem to get things to work. When I have my file put together, it keeps giving me the $end statement and I have checked everything. Is there anything you can tell me to help me here. I have been working on this for almost 3 weeks and I am desperate…!

    Thank you for you time and keep up the great work…!!

    James

  27. Karoma says:

    This is a very nice tutorial, i liked you used the OO. Thanks

  28. James says:

    Very very helpful tutorial btw.

    its will be almost 11 months since this part 1 of the tutorial!! is there a part 2 by any change coming? Would really really appreciate it!

    Thanks

    • Jim Trowbridge says:

      I am also excited to see part 2. I’d like to learn to refactor the classes and best practices regarding hashing passwords and the ‘Forgot Password’ system. I’ve been reading a lot about it and this video has really helped with regard to OOP and prepared statements, since it used a very usable example ( I use authentication systems all the with my applications ).

      Thank you!

      -Jim

    • Seth says:

      OK, I think Part 2 is much overdue too!! I’d love to see it soon!

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

  30. nRadiant says:

    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?

  31. Glenn says:

    Awesome prize. Wouldn’t mind getting one!

  32. Levi V says:

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

  33. thomas says:

    hello cest génial j aime beaucoup

  34. Emil says:

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

    where is the for the beginners version????

  35. Alex McCabe says:

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

  36. Abishek Ravi says:

    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.

  37. phil says:

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

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

  38. Rupert says:

    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 :) .

  39. Drizzy says:

    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

  40. Drizzy says:

    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

  41. PDesignX says:

    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…

  42. Edward says:

    Excellent tutorial for beginners.! I learned a lot.

    When can we see the PART 2?

    Thanks a lot Jeff!

  43. carl says:

    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?

Comment Page 5 of 5 1 ... 3 4 5

Add a Comment