How to:A Simple PHP Login Form Using Mysql

by Neli S Gautham Raj on May 23, 2010

Objective: To create simple PHP Login Form .

member_login

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 .

http://localhost/xammp

2

Step 2 : Create Database ‘test_db’

3

Step 3 : Create table ‘test’ with three fields .

5

Three fields are :

  • Id
  • Username
  • Password

6

After creating Table ‘test’ with three fields , Click save button .

7 copy

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.

8

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 .

11

12

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>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
Output : 
member_login 

 

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

15

Note : Here ‘Login’ is folder name .

Now type the username and password (as we gave in the database) .

14

If username and password is correct then it will display :

login_success

otherwise it will show :

16

That’s it done .

Thank you for visiting my tutorial .

Related posts:

HTML5 news
Free iPhone Programming Ebook
How smart is your Theme?  How good is your support? Check out ThesisTheme for WordPress.

{ 44 comments… read them below or add one }

Rahul May 28, 2011 at 8:10 pm

Very useful post, Nice Share, Keep Posting Bro .. :D

Reply

Gautham Raj August 23, 2011 at 10:40 pm

Thanks Rahul !!

Reply

Manan Narayan June 1, 2011 at 2:19 pm

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

Reply

Dave June 3, 2011 at 9:37 pm

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

Reply

N.S Gautham Raj June 5, 2011 at 12:18 am

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

Reply

mohd khalid June 23, 2011 at 4:40 pm

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

Reply

N.S Gautham Raj June 25, 2011 at 8:28 pm

@Mohd Khalid thank you !!

Reply

fara July 19, 2011 at 7:53 am

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

Reply

Gautham Raj August 23, 2011 at 10:37 pm

Thanks Fara !!

Reply

abhishek July 24, 2011 at 11:24 am

thank you so so much! i am delighted

Reply

N.S Gautham Raj August 23, 2011 at 10:56 pm

Cool … :)

Reply

Divyanshu August 8, 2011 at 6:50 pm

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

Reply

N.S Gautham Raj August 23, 2011 at 10:45 pm

Thank you Divyanshu …

Reply

vahid August 9, 2011 at 2:57 pm

This is great and usefull
thank U

Reply

maria August 17, 2011 at 3:08 pm

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

Reply

N.S Gautham Raj August 23, 2011 at 10:48 pm

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

Reply

Quje October 13, 2011 at 5:34 pm

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

Reply

paeh August 22, 2011 at 2:48 am

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

Reply

Gautham Raj August 23, 2011 at 10:42 pm

Hi Paeh ,

Did you gave “test” as $db_name ??

Reply

Sajan August 23, 2011 at 11:05 pm

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

Reply

Charlie September 14, 2011 at 5:34 pm

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?

Reply

Huda Khaled September 24, 2011 at 8:15 pm

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.

Reply

N.S Gautham Raj October 15, 2011 at 2:24 am

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

Reply

borko December 26, 2011 at 4:46 am

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 ?!

Reply

dohflippa October 6, 2011 at 4:58 pm

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

Reply

Ralph October 11, 2011 at 8:08 pm

mine says it cannot connect to DB. Why?

Reply

Soumyadeep October 15, 2011 at 12:10 am

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

Reply

Sadhu Baba December 5, 2011 at 12:09 pm

“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.

Reply

syerleyza December 9, 2011 at 9:01 am

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

Reply

shashank December 28, 2011 at 8:40 am

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……..

Reply

ankita January 13, 2012 at 12:32 pm

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

Reply

liz January 19, 2012 at 4:02 pm

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

Reply

Mz C-me January 23, 2012 at 11:01 pm

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

Reply

rajarajan February 8, 2012 at 6:55 am

how to logout u didnt mention that

Reply

Puneet March 12, 2012 at 9:09 am

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

Reply

bro March 12, 2012 at 1:31 pm

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

Reply

Jack March 14, 2012 at 11:59 pm

Works great, thanks!

Reply

tnam March 19, 2012 at 9:34 pm

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

Reply

azz April 5, 2012 at 6:40 am

i have same problem like bro..can help me

Reply

nirm April 22, 2012 at 6:49 pm

Nice post Bro keep posting

Reply

ganesh April 24, 2012 at 2:58 pm

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

Reply

Joseph May 5, 2012 at 3:10 pm

Very useful tutorial. very simple to follow

Reply

lupo May 15, 2012 at 10:22 pm

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

Reply

cris May 17, 2012 at 6:43 am

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

Reply

Leave a Comment

{ 1 trackback }

Previous post:

Next post: