Objective: To create simple PHP Login Form .
Prerequisite :
In order to create simple PHP login form , we are going to use following tools .
- Notepad ++
- Xammp
- Google Chrome /Any Browsers
Step by Step Tutorial :
Step 1: Go to following link and click PhpmyAdmin is which under Tools menu .
Step 2 : Create Database ‘test_db’
Step 3 : Create table ‘test’ with three fields .
Three fields are :
- Id
- Username
- Password
After creating Table ‘test’ with three fields , Click save button .
Step 4: Till step3 we have created Database and table name . Next we are going to insert the values in the table.(username and password).
We can insert in two ways :
- One way is directly which is same as we created the table .
- Second way is by inserting the SQL query.
Step 5 :Create four PHP files as mentioned below & Open notepad ++ and paste following php codes into it . Finally dont forget to save these files into htdocs folder .(It is present in XAMMP folder)
- main_login.php
- check_login.php
- login_success.php
- logout.php
Let’s create a folder say Login in htdocs folder , in which we will save all these php files which is mentioned above .
main_login.php
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"> <tr> <form name="form1" method="post" action="check_login.php"> <td> <table width="100%" border="0" cellpadding="3" cellspacing="1"
bgcolor="#FFFFFF"> <tr> <td colspan="3"><strong>Member Login </strong></td> </tr> <tr> <td width="78">Username</td> <td width="6">:</td> <td width="294"><input name="myusername" type="text" id="myusername">
</td> </tr> <tr> <td>Password</td> <td>:</td> <td><input name="mypassword" type="text" id="mypassword"></td> </tr> <tr> <td> </td> <td> </td> <td><input type="submit" name="Submit" value="Login"></td> </tr> </table> </td> </form> </tr> </table>
Output :
check_login.php
<?php $host="localhost"; // Host name $username=""; // Mysql username $password=""; // Mysql password $db_name="test"; // Database name $tbl_name="members"; // Table name // Connect to server and select database. mysql_connect("$host", "$username", "$password")or
die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB"); // username and password sent from form $myusername=$_POST['myusername']; $mypassword=$_POST['mypassword']; // To protect MySQL injection (more detail about MySQL injection) $myusername = stripslashes($myusername); $mypassword = stripslashes($mypassword); $myusername = mysql_real_escape_string($myusername); $mypassword = mysql_real_escape_string($mypassword); $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and
password='$mypassword'";
$result=mysql_query($sql); $count=mysql_num_rows($result); if($count==1){ session_register("myusername"); session_register("mypassword"); header("location:login_success.php"); } else { echo "Wrong Username or Password"; } ?>
login_success.php
<? session_start(); if(!session_is_registered(myusername)){ header("location:main_login.php"); } ?> <html> <body> Login Successful </body> </html>
logout.php
<? session_start(); session_destroy(); ?>
How to run :
Go to browser and type
Note : Here ‘Login’ is folder name .
Now type the username and password (as we gave in the database) .
If username and password is correct then it will display :
otherwise it will show :
That’s it done .
Thank you for visiting my tutorial .




{ 44 comments… read them below or add one }
Very useful post, Nice Share, Keep Posting Bro ..
Thanks Rahul !!
I get “Wrong Username or Password” for all credentials.
This is ridiculous as you can just type in the success page address in the browser and it works….
@Manan try Again !! It worked for me and some of my friends also got output !! Where you got stuck ??
very good modifeid in our lauguage and create the login form step by step i am very happy for you
@Mohd Khalid thank you !!
ya TRUE..very useful..teach me alot..
Thanks Fara !!
thank you so so much! i am delighted
Cool …
great work buddy… if any one is getting error using header in check_login.php… u can use echo “”;
Thank you Divyanshu …
This is great and usefull
thank U
its was great to implement……. thanks a lot.
Its great to hear such comments ..thank you maria ..
Its cool bro do it again and make simpler more and more
hey, i got this ‘cannot select db’, help me pleasseeee
Hi Paeh ,
Did you gave “test” as $db_name ??
Suppose if I access login_success.php directly, without logging in…it is coming up…but it should not come up rt?
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?
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.
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
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 ?!
hi guys i am getting this ‘cannot select db’, help me pleasseeee
mine says it cannot connect to DB. Why?
Thank u Raj,it’s very easy&working.plz keep writing on more topics…. )
“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.
i’m getting an error like this : “Fatal error: Call to undefined function: mysql_real_escape_string()”. so how to solve it?
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……..
I have done it,and it is working. Thanks.!
Thank you Raj, it’s very useful. Please keep on posting more.!!!
after typing in my username and password…it doesn’t show anything
how to logout u didnt mention that
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
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
Works great, thanks!
help me ! “cannot connect” on a check_login.php
i have same problem like bro..can help me
Nice post Bro keep posting
Parse error: syntax error, unexpected T_VARIABLE in C:\xampp\htdocs\login\check_login.php on line 32
Very useful tutorial. very simple to follow
i have two error like this, how to fix this error?
Notice: Use of undefined constant myusername – assumed ‘myusername’ in D:\xampp\htdocs\login_sample\login_success.php on line 3
Deprecated: Function session_is_registered() is deprecated in D:\xampp\htdocs\login_sample\login_success.php on line 3
Login Successful
got wrong username and password even if my username and pass is correct.
{ 1 trackback }