65 Comments

Leave us a comment

  • Galal Salib
    on

    Thanks!

  • Victor
    on

    Thanks, very useful for a beginning php scripter,
    i’m gonna use it and expand with other fields!

  • Mark
    on

    Very useful and straight to the point! Thanks for this.. it got me up and running in the space of 10 minutes :)

  • julius
    on

    codes send me

  • mubarak
    on

    it is very useful,thanks a lot!

    • Frost
      on

      Thanks for pointing that out. Somehow I missed the .zip part, it should work now.

  • mark
    on

    sir it says

    Warning: require(C:/wamp/www/login/includes/config.php) [function.require]: failed to open stream: No such file or directory in C:wampwwwWARsluindex.php on line 7

    Fatal error: require() [function.require]: Failed opening required ‘C:/wamp/www/login/includes/config.php’ (include_path=’.;C:php5pear’) in C:wampwwwWARsluindex.php on line 7

    • Jolin
      on

      I have the same problem with Mark,
      May I know how to solve this problem?

  • John
    on

    Your hashPassword() function is insecure. Hashing a hash reduces the security of the hash by increasing collisions. Also, using the same salt(s) for every password is just as bad as no salt. Also, md5() and sha1() are and were never intended for password storage. The correct way to store passwords is to use a bcrypt-style hashing algorithm.

    You should link users to the following excellent login system:

    http://barebonescms.com/documentation/sso/

    It has more features than you can shake a stick at. I also don’t know of anything its equal. A tutorial is fine for an exercise but that product just kicks butt – especially when it comes to security.

  • zios
    on

    Hi!
    I am totally new to web developement and when I type on my browser localhost/login.php nothing appears. I tried with all the files and I cannot make the login page work… what should I do?

    How do I make the login form appear when I go to my localhost/….?

    Sorry for my ignorance. I am just starting a class in University and I am a new , really new noob in this field.

    Thanks for your answers!

    • Jim
      on

      Hi zios,

      What webserver are you using? What Operating are you using?

      You will need to install a webserver of some type like Apache, Lighttpd or Nginx, and you will also have to integrate the webserver to parse PHP Pages. If you are on Windows, I would suggest WAMP for beginning as it does most of the hard stuff for you.

  • Phil
    on

    Your download seems to be broken.
    Nice tutorial though.

    • Jim
      on

      Ah thanks for pointing that out. The download link has been updated and should work properly now.

  • John
    on

    Hello! Nice tutorial. But how i can control users on pages for that is they logd in or not? I think i must have some script every page what i have to control with password?

    • Jim
      on

      That is what sessions are for, which I think I skipped over in the tutorial. The session will tell us who is logged in or not, so anywhere you see $_SESSION, that is what is happening. Sorry for skating over that.

  • kamran
    on

    Hello Bravo!
    I am in beginner of php and web system .
    so can we simply copy and paste this code to make our login and registeration page.

    • Jim
      on

      Copy / Paste or download the .zip file on the last “Overview” page, which has it all linked up.

  • k
    on

    Hi, I dont understand something. You wrote the several functions, ex the config, register and etc. So how do you establish connection with the server and get user registration at the same time? I mean the php codes are in separate blocks, so do you just simply connect them together and paste? I wrote mine and i was kinda confused.

  • Jim
    on

    @k, yes. But on the Overview page, there should be a link at which you can download the full code all linked together to see how it fits.

    Hopefully that will work out for you, Thanks!

  • Gerry
    on

    Hi Nicely laid out and explained. Worked out of the box. However i’m seeing the mysql being deprecated in the future, and and going to modify to use mysqli OO. Might be worth updating the sample. (or comment the changes). Cheers.

    • Jim
      on

      I will update them / create a new post with the updated methods. Thanks for the suggestion.

  • wizard
    on

    ok this doesn’t work on my environment.

    as this is a shared server all websites are under the public_html directory

    this line needs to be changed to??

    require($_SERVER[‘DOCUMENT_ROOT’] . ‘login/includes/config.php’);

    this line needs to be changed to??

    and i probably missed this but i don’t see where its being used.

    in the config.php
    $sFolder = ‘login’;

    thank you for some feedback

    • Jim
      on

      The $sFolder is being used in the require line inside the config file. As for what your line needs to be, echo out $_SERVER[‘DOCUMENT_ROOT’] and figure out what that is and then adjust the path as necessary, it could be your hosting environment does not add the ending / to that variable and you just need a / prior to login.

  • Luke
    on

    Should use PDO to connect to the database. mysql_connect() has become depreciated (see php.net), so this tutorial is out-dated also. The concept is fine though.

    • Jim
      on

      I would agree Luke, MySQLi or PDO would be a better option. Maybe I will update the tutorial and or write an updated tutorial version.

      For now, the concept is what matters :)

  • Dale
    on

    I have the same problem as Wizard. I am definitely sure I have placed the contents in the Root Document, but I still obtain the same errors:

    Warning: require(C:\inetpub\wwwrootlogin/includes/config.php): failed to open stream: No such file or directory in C:\inetpub\wwwroot\index.php on line 7

    Fatal error: require(): Failed opening required ‘C:\inetpub\wwwrootlogin/includes/config.php’ (include_path=’.;C:\php\pear’) in C:\inetpub\wwwroot\index.php on line 7

    When opening a file.

    • Jim
      on

      Change this line:
      require($_SERVER[‘DOCUMENT_ROOT’] . ‘login/includes/config.php’);

      To be:
      require($_SERVER[‘DOCUMENT_ROOT’] . ‘/login/includes/config.php’);

      And that should resolve your issue, they just setup DOCUMENT_ROOT variable different than my webserver has it.

  • jameel
    on

    I really liked this tutorial and I used it on my website. However, I read the comments and did some searching and I don’t understand why I have to use PDO instead of mysql functions. And what do you really mean by deprecated? does it mean that it is simply not recommended or that it is insecure or something.

    I found this tutorial: http://www.sunnytuts.com/article/login-and-registration-with-object-oriented-php-and-pdo that also shows how to create a log in system, but using object oriented PHP and PDO. I’m not comfortable with either of the two as I also code in procedural.

    So can you please tell me if I can keep your code, or I have to change it to PDO, or follow that tutorial? Please let me know about this and thank you very much.

    • Jim
      on

      Depreceated means that the functions will be removed in a later version and as of PHP 5.5 (I believe or 5.6) the mysql functions have been depreciated for their mysqli counter parts. You do not HAVE to use PDO, but I find PDO much easier to work with and to secure over mysqli. Especially since you can write a nice DB Wrapper for your prepared statements in PDO and only have to use 1 line to call it, instead of with the mysqli, you have to write 5+ lines each time for the prepared statements.

  • emmy
    on

    am new to php but this has been my best retrieval from the internet so far. thanks a lot and God bless you

    • Jim
      on

      Thank you emmy.

  • David Grossman
    on

    I’m having a couple issues (new to php) and trying to get it running on wordpress. If you have 5-10 minutes, and you e-mail so we could chat?

  • colby
    on

    FINALLY….

    A Freaking Tutorial that actually works…

    I’m No expert in php by any means and this will help me out tons!!!!

    Thank you soooo much.

    • Jim
      on

      Glad it helped, that was the point of it!

      Thanks.

  • vishal
    on

    thanks buddy for this script. you saved my time.. :)

  • bogs
    on

    Warning: require_once(C:/xampp/htdocsloginincludes/functions.php): failed to open stream: No such file or directory in C:\xampp\htdocs\login\includes\config.php on line 32

    Fatal error: require_once(): Failed opening required ‘C:/xampp/htdocsloginincludes/functions.php’ (include_path=’.;\xampp\php\PEAR’) in C:\xampp\htdocs\login\includes\config.php on line 32

    • UnkownJ
      on

      Check your files, i think you got this in all your login files :
      require($_SERVER[‘DOCUMENT_ROOT’] . ‘login/includes/config.php’);

      Change it to this:
      require($_SERVER[‘DOCUMENT_ROOT’] . ‘/login/includes/config.php’);

      I think it will work then ;P

  • Alex
    on

    Really useful :D That’s what I wanted. Thank you very much!

  • Ignacio Aular
    on

    Hello everybody! we need to put a slash to the beginning of the word ‘login’ in the path, example:

    File: login.php at Line: 7

    Before:
    require($_SERVER[‘DOCUMENT_ROOT’] . ‘login/includes/config.php’);

    After:
    require($_SERVER[‘DOCUMENT_ROOT’] . ‘/login/includes/config.php’);

    The same thing must be modified in register.php at line 7 and finally in config.php at line 32, that’s all guys!

    Best regards!

    • Jim
      on

      Given that the “DOCUMENT_ROOT” is specified by the server configuration file, this may or may not be true. Depending on how and who sets up your server and specifies the DOCUMENT_ROOT, that can change.

      Thanks for pointing out that it may need the beginning /, however, this is not true of all setups.

  • Zoë
    on

    I have this strange thing. When someone wants to make an account, it doesn’t create in the db.
    I did change something but I don’t think that’s the problem. I also got the same error as bogs, but I just put the 2 php files in includes, without the login folder. so I just removed the entire $sFolder = ‘login’ usage.

    But I can’t get the accounts into the db, and I really don’t know why. Also, the only error I get on the register.php page, is when I use less than 4 letters. When I don’t fill in the password, fill in everything or don’t fill in anything, the page just refreshes? No warning, and no created account. Does anybody know what’s going on here?

    • Jim
      on

      Hi Zoe, It is really hard to diagnose an issue without knowing what server OS you are running, what Web Server software you are running and without looking at the actual error.log (which should be in /var/logs//error.log pending on Linux Operating System). If you would like more detailed help, feel free to use the Contact page to send me an email and I will be happy to help you work out the issue.

  • Ning
    on

    Hi Jim, I found the session variables $_SESSION[‘username’] and $_SESSION[‘loggedin’] are kind of redundant. What if just use $_SESSION[‘username’] only? If a user is logged in, the $_SESSION[‘username’] is set to the username. If not logged in, it’s null or unset. To me, the variable $_SESSION[‘loggedin’] could be eliminated. What do you think? Is there any fact regarding security I didn’t think of? Cheers!

    • Jim
      on

      Agreed. Thanks for taking the time to point that out. I will be creating an updated tutorial this week which will address a few issues pointed out and the deprecation of mysql_* commands in lieu of mysqli_*.

      As far as security I do not think you missed anything but it is late and I cannot be 100% sure. Either or the new tutorial should be better with up to date information and code edits.

  • Shanaya
    on

    Hello,

    After putting the slash at the beginning of the login in all the required files i’m still having the same problem :( help please

    Warning: require(C:/xampp/htdocs/login/includes/config.php) [function.require]: failed to open stream: No such file or directory in C:\xampp\htdocs\test\index.php on line 7

    Fatal error: require() [function.require]: Failed opening required ‘C:/xampp/htdocs/login/includes/config.php’ (include_path=’.;C:\xampp\php\pear\’) in C:\xampp\htdocs\test\index.php on line 7

    • Jim
      on

      Hi Shanaya,

      It looks like you need to change the “login” to be “test” and or rename “test” to be “login” and that should fix your issue. Let me know if it doesn’t and I can help you further.

  • Shanaya
    on

    Hi,

    I have renamed the “test” to “login” but still having this problem
    Warning: require_once(C:/xampp/htdocsloginlogin/includes/functions.php) [function.require-once]: failed to open stream: No such file or directory in C:\xampp\htdocs\login\includes\config.php on line 32

    Fatal error: require_once() [function.require]: Failed opening required ‘C:/xampp/htdocsloginlogin/includes/functions.php’ (include_path=’.;C:\xampp\php\pear\’) in C:\xampp\htdocs\login\includes\config.php on line 32

  • chris
    on

    do i have to create a table field session to store session id for every user.

  • Brendan
    on

    Perfectly cooked tipped – Works a charm and offers an excellent jump off point for a wider project.

  • Akash
    on

    your download link is not working please provide me the sip of the complete module…
    when i copied the code from above and make it run on my machine than it is showing blank index page please help urgent. :)

  • MishuAnubis
    on

    I have same errors like Shanaya and i cant fixed them at all :(

  • walterg
    on

    everything works great!
    how can I use css and html to add some stuff in login.php?

  • trewin
    on

    Hi ,

    I’m Trewin. I am learning to develop websites . Your Tutorial was really helpful for me.

    Though i give the username and password correctly its not getting logged in .The Message “Bad Username and Password supplied” is poping out each and every time when im trying to login.
    but the username and password has got registered correctly in the database.

    Can you just let me know what should i do?

  • Jake
    on

    Thanks man, this is the only login/register system what works on my home server, I hope to expand with this code as i’m a quick learner, thanks millions

  • Jadav
    on

    its showing this error message

    Warning: require(C:/xampp/htdocs/login/includes/config.php) [function.require]: failed to open stream: No such file or directory in C:\xampp\htdocs\test\index.php on line 7

    But I have found another resource here which is very simple and easy to understand so can you please make exactly the same here because I am really like to use only your blog.

  • guest
    on

    thanks for the great tutorial but you said “I will be creating an updated tutorial this week” in march. so i wonder when will this update coming?

  • Locke
    on

    I tried to use this tutorial and many others to implement this kind of feature to a website, however none of them work.
    I resorted to downloading your .zip and when I ran the tested the files using MAMP it did not show any HTML code, text or anything. This was the same for all my other attempts.
    It was all hidden.
    I am frustrated and ready to quit unless I can find a solution ASAP
    None of my friends or colleagues can help, none of my google searches turned up anything relevant.
    If you can’t help me then I will have to abandon hope.

  • Jessica
    on

    How would I go about allowing the user to “unregister”? I tried to alter the code so that the register function would work in the opposite way by deleting the user by email and username, however am struggling to get the code functioning properly. Any suggestions?

  • EMMANUEL OKELLO
    on

    helped a lot, just want to know how i can add json so that i can connect this to my android app using the external database. cud u do a tutorial on that?

  • Marea Norton
    on

    Hi Jim
    I know a little php and can follow your registation/login script, (and its great) but I cannot get it up in my browser.
    My apache DocumentRoot is “var/www/html”. My path to your script is localhost/skunk/register.php and I get NOTHING ,just a blank page. I have other scripts that I run from folders in my var/www/html folder and they run okay.
    I also read some of the comments and put a / in front of login on all the files that required all to no avail.
    I have noticed that you are kind enough to answer queries, so I will wait in anticipation for your answer.
    Regards Marea (Australia)

    • Jim
      on

      When a PHP Script causes a blank page, it is best to look at the error logs for the web server. (/var/log/apache2; /var/log/nginx) and see where it is throwing the error. The must likely culprit is the mysql commands as I believe PHP has deprecated them in lieu of mysqli commands.

      When I have some free time I will re-write the tutorial for mysqli. For now, you should be able to look up mysqli usage and edit the script with that in mind.

  • kiros
    on

    thank you for your great work, the reward that can i give you is just keep it up!!! thank you again.

  • Roger
    on

    I have a few questions. Actually I’m pretty much just a beginer in php and mysql, I’m curently learning them in school but I don’t have the leisure to wait for until I learn all the things that I need. I didn’t understand some of the things above, mostly because there are some sintax terms(something like isset, switch, $_GET) because I didn’t learned them yet but I mostly understood how it works so far. What I want is that for example if I create a page index.php let’s say and I create a few more pages linked to index.php, when I log in on index page or log in page, I want to be logged in on any page I visit in that domain, or does this script already do that? also I’d like to add something like a navigation bar where the user can see his name if he’s logged in or the log in/register buttons if not(for all the pages in the domain/folder as well). Another thing would be to be able group users somehow like: admin,moderator,normal user etc. or something like that and give a set of permissions to each group. Are these thing possible? If yes, can someone help me a bit and explain them to me? I know my way around html and css pretty well so there’s no problem with those. Sorry for being a pain if I was.

Leave a Reply to Roger Cancel Reply

↑ BACK TO THE TOP ↑