113 Responses to “How to:A Simple PHP Login Form Using Mysql”
  1. Rahul

    Very useful post, Nice Share, Keep Posting Bro .. 😀

  2. Manan Narayan

    I get “Wrong Username or Password” for all credentials.

    • nidhi

      me toooo :(:( plzz help

  3. Dave

    This is ridiculous as you can just type in the success page address in the browser and it works….

  4. N.S Gautham Raj

    @Manan try Again !! It worked for me and some of my friends also got output !! Where you got stuck ??

  5. mohd khalid

    very good modifeid in our lauguage and create the login form step by step i am very happy for you

  6. N.S Gautham Raj

    @Mohd Khalid thank you !!

  7. fara

    ya TRUE..very useful..teach me alot..

  8. abhishek

    thank you so so much! i am delighted

  9. Divyanshu

    great work buddy… if any one is getting error using header in check_login.php… u can use echo “”;

  10. vahid

    This is great and usefull
    thank U

  11. maria

    its was great to implement……. thanks a lot.

    • N.S Gautham Raj

      Its great to hear such comments ..thank you maria ..

    • Quje

      Its cool bro do it again and make simpler more and more

  12. paeh

    hey, i got this ‘cannot select db’, help me pleasseeee

    • Gautham Raj

      Hi Paeh ,

      Did you gave “test” as $db_name ??

  13. Sajan

    Suppose if I access login_success.php directly, without logging in…it is coming up…but it should not come up rt?

  14. Charlie

    Great, easy steps, but I can’t get it to work!

    Keeps coming up with “Wrong Username or Password”, yet I only have 2 records in my database.

    Also, the login_success.php page is readily available to the user if they enter it in the address bar, I suggest using login_success.php as a redirect page to another page that requires a cookie to access maybe?

  15. Huda Khaled

    I am getting an error “Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given”. I do not know how to solve it.

    • N.S Gautham Raj

      Hi Huda ,

      I think you created the table but you forget to insert the values . Please try again and let me know in case of any issues .

      Regards ,
      Gautham

    • borko

      Hi i am getting the same error as Huda

      i have the table and the user and password till this part
      $sql=”SELECT * FROM $tbl_name WHERE user=$myusername AND password=$mypassword”;

      from this point on the error is shown. is it something from with the query or what ?!

      • Neli S Gautham Raj

        Hi Huda & Borko :

        Possible reason is mismatch of table name . For example , In MySQL database you have given X as table name but in PHP you have given Y as table name .

        Please let me know your thoughts .

  16. dohflippa

    hi guys i am getting this ‘cannot select db’, help me pleasseeee

  17. Ralph

    mine says it cannot connect to DB. Why?

    • venkat

      check the my sql database name and checklogin.php code . mismatching the two entires………..

  18. Soumyadeep

    Thank u Raj,it’s very easy&working.plz keep writing on more topics…. )

    • amal g

      plz help me, my result is getting a warning.”Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\php\checking.php on line 31″

      31th line code is: ” $count = mysql_num_rows($result); ”

      any problem?

      • Neli S Gautham Raj

        Hi Amal g ,

        Possible reason is mismatch of table name . For example , In MySQL database you have given X as table name but in PHP you have given Y as table name .

        Please let me know your thoughts .

  19. Sadhu Baba

    “This is ridiculous as you can just type in the success page address in the browser and it works….”

    This is not a secure way to setup a login, but it is a good intro. There needs to be a way to implement security for multiple pages so users can’t just save the url and have at it.

    • Neli S Gautham Raj

      Hi Sadhu Baba,

      Ya you are correct .. This is not secure . It is for beginners .

      Thanks ,
      Gautham

  20. syerleyza

    i’m getting an error like this : “Fatal error: Call to undefined function: mysql_real_escape_string()”. so how to solve it?

  21. shashank

    Thank You Thank You Very much….i have been trying to fetch the data from database from long time but i didnt sucseed….but with the use of this code i easily fetch data from database without any error……thank you thank you soooooooooooo much……..

  22. ankita

    I have done it,and it is working. Thanks.!

  23. liz

    Thank you Raj, it’s very useful. Please keep on posting more.!!!

  24. Mz C-me

    after typing in my username and password…it doesn’t show anything

  25. rajarajan

    how to logout u didnt mention that

    • Gautham

      Hi ,

      Please try with this code in Logout.php :

      1
      2
      3
      4
      5
      6
      7
      8
      < ?
      session_start();
      session_destroy();
      if(session_destroy())
      {
      header("Location:main_login.php");
      }
      ?>

      Thanks,
      Gautham

  26. Puneet

    Yah………..It works also for me thank you MR ….But i did not know how to logout in this page …..please help me thanx again

    • Nisha

      Two steps to be followed.

      1.Add the below script in login_success.php (after the message login Success)

      1
       <a href="logout.php" rel="nofollow">Sign Out</a>

      2.Replace the script in logout.php using the script provided below.

  27. bro

    i have problem logging in!
    i have this error!
    Warning: mysql_connect() [function.mysql-connect]: Access denied for user ‘ODBC’@’localhost’ (using password: NO) in C:\xampp\htdocs\login\check_login.php on line 9
    cannot connect

    • amal g

      can u check ur user name and password:
      example my user name =root and password is not set.
      so code is:
      $host=”localhost”; // Host name
      $username=”root”; // Mysql username
      $password=””; // Mysql password

      try it , like…!

  28. Jack

    Works great, thanks!

  29. tnam

    help me ! “cannot connect” on a check_login.php

    • Neli S Gautham Raj

      Hi Tnam & Azz,

      Please check whether database name , table name , username & password of PHP matches with MySQL .

      Thanks,
      Gautham

  30. azz

    i have same problem like bro..can help me

  31. nirm

    Nice post Bro keep posting

  32. ganesh

    Parse error: syntax error, unexpected T_VARIABLE in C:\xampp\htdocs\login\check_login.php on line 32

  33. Joseph

    Very useful tutorial. very simple to follow

  34. lupo

    i have two error like this, how to fix this error?

    Notice: Use of undefined constant myusername – assumed ‘myusername’ in D:xampphtdocslogin_samplelogin_success.php on line 3

    Deprecated: Function session_is_registered() is deprecated in D:xampphtdocslogin_samplelogin_success.php on line 3
    Login Successful

    • Olivia

      I have the same error. If anyone knows how to fix this, please help.

      I did find this link: http://stackoverflow.com/questions/2600905/php-alternative-to-session-is-registered

      But I still can’t get it to work.

      • Olivia

        However, I’m pretty sure it still works. If so, just add:

        ini_set(‘display_errors’, 0);

        This will make sure the browser does not show the error.

        • Olivia

          OKAY, sorry, 3rd post but I did get this to work. Using the site I linked to earlier, I came up with this solution:

          • Olivia

            session_start();
            //ini_set(‘display_errors’, 0);
            $myusername=$_SESSION[‘myusername’] ;

            if(isset($_SESSION[$myusername])){
            header(“location:main_login.php”);
            }

          • Neli S Gautham Raj

            Thanks Olivia .. !! You came up with correct solution .. Thanks again ..!!

    • Neli S Gautham Raj

      Hi Lupo ,

      Sorry for late response .

      Please replace following code in Check_login.php

      if($count==1){
      session_register(“myusername”);
      session_register(“mypassword”);
      header(“location:login_success.php”);
      }

      with

      if($count==1){
      session_start();
      $_SESSION[‘username’] = ‘myusername’;
      $_SESSION[‘password’] = ‘mypassword’;
      header(“location:login_success.php”);
      }

      This is happening as session_register function is deprecated in PHP’s new versions .

      Hope this works .

  35. cris

    got wrong username and password even if my username and pass is correct.

    • Neli S Gautham Raj

      Hi Cris ,

      Thanks for comments .

      Please check table values which you inserted in MySQL table and let me know if you are still getting the same issue .

      Thanks,
      Gautham

  36. Neha

    Its great but could not connect to the database tell me why?

  37. Boris

    I get a error “Database connection failed: Unknown database ‘test'”
    What’s up with that???

    • Jeyaganesh

      Please check whether the ‘test’ database is created.

  38. Sanjana

    Am new in php.This code is very much informative..I got the output…
    thanks a lot….
    But i did not know how to logout in this page
    …please help me thanks again…..

    • Gautham

      Hi ,

      Please try with this code in Logout.php :

      Thanks,
      Gautham

  39. rohan

    keep posting bro

  40. timothy

    Hi, im getting this error
    Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\wamp\www\login\check_login.php on line 27

    • Neli S Gautham Raj

      Hi Timothy,

      Whether you inserted min 1 set of values in your table . It is mandatory to insert the values in the table before you do execute the whole code .

      Hope this helps ..If it doesn’t works .. Please let me know ..

      Thanks,
      Gautham

    • Neli S Gautham Raj

      Another possible reason is mismatch of table name . For example , In MySQL database you have given X as table name but in PHP you have given Y as table name .

      Please let me know your thoughts .

  41. Silendu

    THANK YOU VERY MUCH!!!!!

  42. soundhar

    thanx… this is what i searching for many days… well thanx…

  43. asmida

    i’m getting error like this.

    Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\Login\check_login.php on line 27
    Wrong Username or Password

    how to do?

    • Neli S Gautham Raj

      Hi,possible reason is mismatch of table name or database name . For example , In PHPmyadmin MySQL database you have given X as table name but in PHP code you have given Y as table name .

  44. dania

    how to fix this?

    Object not found!

    The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.

    If you think this is a server error, please contact the webmaster.

    Error 404

    localhost
    03/08/2012 12:19:47
    Apache/2.2.17 (Win32) mod_ssl/2.2.17 OpenSSL/0.9.8o PHP/5.3.4 mod_perl/2.0.4 Perl/v5.10.1

    • Jeyaganesh

      Dania,
      check what have you given for action attribute

      have you created check_login.php?
  45. Pravin Dasari

    when i submit login page this error is coming

    Warning: mysql_connect() [function.mysql-connect]: Access denied for user ‘ODBC’@’localhost’ (using password: NO) in C:\xampp\htdocs\training\php_site\login\check_login.php on line 9
    cannot connect

    Please help

    • Jeyaganesh

      Pravin,
      Check your database connection parameters like username and password.
      Thanks.

  46. Quek Quek

    oh man! thankz for the help ~ 🙂 really useful for me.
    I need more and more to do my final year project
    because i have no knowledge about php. 🙁

  47. jeeva

    Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\wamp\www\samcollege\check_login.php on line 23
    Wrong Username or Password

    $sql=”SELECT * FROM $test WHERE username=’$myusername’ and password=’$mypassword'”;
    these errors are occured what can i do?

    • Jeyaganesh

      Hi,
      There is a query error in your code.Try to echo the mysql query and execute in phpmyadmin to check the error.
      it usually occurs when the query execution returns false.
      Thanks.

  48. Rajeev

    Hi Gautham, Thanks for the beautiful post. It is working well for me.
    What can I do if I want the users to be directed 2 different landing pages. eg if admin logs in then the admin lands on a different page and if another user logs in then he lands on a different page. I cant seem to work with the header function to create this. Your help will be appreciated. thanks.

  49. mangesh

    Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\wamp\www\samcollege\check_login.php on line 23
    Wrong Username or Password

    this type of error is occured so what can i do for this?

    please send me reply queckly……..

    • Gautham

      Hi Mangesh,

      Possible reason is mismatch of table name or database name . For example , In PHPmyadmin MySQL database you have given X as table name but in PHP code you have given Y as table name .

      Thanks,
      Gautham

  50. Tish

    after doing all that this is wat i get after trying to login…………

    ( ! ) Warning: mysql_connect() [function.mysql-connect]: Access denied for user ”@’localhost’ (using password: NO) in C:\wamp\www\tests\login\checklogin.php on line 10
    Call Stack
    # Time Memory Function Location
    1 0.0260 375096 {main}( ) ..\checklogin.php:0
    2 0.0260 375800 mysql_connect ( ) ..\checklogin.php:10
    error cannot connect

    please help

    • Gautham

      Hi Tish ,

      Did you activated Apache & Mysql module in Xammp control panel ? If it doesn’t works .. Please paste your version of check_login.php in comments .. we will sort out the issue .

      Thanks,
      Gautham

  51. Orlando Gautier

    Hello!

    I´m trying this code, but i get this errors:

    ( ! ) Notice: Use of undefined constant myusername – assumed ‘myusername’ in C:\wamp\www\login\login_success.php on line 3
    Call Stack
    # Time Memory Function Location
    1 0.0003 366240 {main}( ) ..\login_success.php:0

    ( ! ) Deprecated: Function session_is_registered() is deprecated in C:\wamp\www\login\login_success.php on line 3
    Call Stack
    # Time Memory Function Location
    1 0.0003 366240 {main}( ) ..\login_success.php:0

    Could you help me please.

    Regards

    • Gautham

      Hi Orlando ,

      Please replace following code in Check_login.php

      if($count==1){
      session_register(“myusername”);
      session_register(“mypassword”);
      header(“location:login_success.php”);
      }

      with

      if($count==1){
      session_start();
      $_SESSION[‘username’] = ‘myusername’;
      $_SESSION[‘password’] = ‘mypassword’;
      header(“location:login_success.php”);
      }

      This is happening as session_register function is deprecated in PHP’s new versions .

      Hope this works .

      • tshephang

        after solving the above error i have this parse error below.how can i solve it.
        Parse error: syntax error, unexpected $end in C:\xampp\htdocs\testing login\login_success.php on line 12

  52. PRABAKARAN

    very superb .this is uses for my career starting in a good way
    thank you soo much

    • Neli S Gautham Raj

      Hi PRABAKARAN,

      Good to hear ! Keep visiting our blog for more updates !!

      Thanks,
      Gautham

  53. srinu

    thanks for nice information about php…

  54. sagar

    Fatal error: Call to undefined function session_register() in C:\xampp\htdocs\loginpage\check_login.php on line 40

    • Jeyaganesh

      session_register() is depreciated as of PHP 5.3.0. You can use $_SESSION.

      • dang.viet.son

        thank you , $_SESSION[“name”]

  55. gabz

    when i login it display a blank screen(white)

  56. vincent

    Warning: mysql_connect() [function.mysql-connect]: Access denied for user ‘ODBC’@’localhost’ (using password: NO) in E:\xampp\htdocs\login\check_login.php on line 17
    cannot connect

  57. ayushman

    hello i am new to php.I like this kind of post where everything explained step by step and i really appreciate the way you have explained.anyway i m facing an error session_register is undefined please help

    • Neli S Gautham Raj

      Hi Ayushman,

      Thanks !!

      Please replace following code in Check_login.php

      if($count==1){
      session_register(“myusername”);
      session_register(“mypassword”);
      header(“location:login_success.php”);
      }

      with

      if($count==1){
      session_start();
      $_SESSION[‘username’] = ‘myusername’;
      $_SESSION[‘password’] = ‘mypassword’;
      header(“location:login_success.php”);
      }

      This is happening as session_register function is deprecated in PHP’s new versions .

      Hope this works .

  58. Ryan

    Just one question, if I not working it in localhost, what I need to type in the “Host name” if for example my domain is http://www.example.com and my web host is http://www.host.com?

    • Jeyaganesh

      Ryan,

      You need to check your server’s php directive which is undefined by default.Try to use localhost and let us know the status.

      Thanks.

  59. ranjitkumar

    hi i am trying your code i got the main_login.php worked fine and after typing my username and password i got this error:
    Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in D:\xampp\xampp\htdocs\check_login.php on line 27
    Wrong Username or Password

  60. Daniel

    Thank You for the codes……It run’s properly…..

  61. sam

    this website is useful for creating login page.

  62. Filipe Beja

    Can you help me ?

    Parse error: syntax error, unexpected ‘:’ in C:\Program Files\EasyPHP-12.1\www\intranet\login\check_login.php on line 33

    It reffers to
    header(“location:login_success.php”);

    Regards and thanks in advange

  63. collins

    Hi,
    i need some help to resolve this problem wenn i test main_login.php:

    Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\login\check_login.php on line 28
    Wrong Username or Password

    thanks in advance

  64. ram

    Undefined index: mypassword in C:\wamp\www\test\checklogin.php

    • Jeyaganesh

      Looks like you are not receiving the password variable during form submit.

      1
       if(isset($_POST['mypassword'])){ $name = $_POST['mypassword']; }

      try this before accessing the password variable.

  65. anil kachhadiya

    Hey, This one is very useful for beginner like me.
    It looks so clear and easy when i find this.
    Thank you so much.

  66. khushboo

    hey,
    i m getting an error
    Fatal error: Function name must be a string in C:\xampp\htdocs\Login\check_login.php on line 31
    could you plz help to solve it.

  67. John

    When i click “submit” i am shown the check_login.php file in my browser.

Leave a Reply

*