Learn to create a simple login system with php + mysql script, this tutorial easy to follow, teach you step by step.
Helpful Links
Overview
In this tutorial create 3 files
1. main_login.php
2. checklogin.php
3. login_success.php
Step
1. Create table “members” in database “test”.
2. Create file main_login.php.
3. Create file checklogin.php.
4. Create file login_success.php.
5. Create file logout.php
Create table “members”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
<p>CREATE TABLE `members` (<br> `id` int(4) NOT NULL auto_increment,<br> `username` varchar(65) NOT NULL default '',<br> `password` varchar(65) NOT NULL default '',<br> PRIMARY KEY (`id`)<br> ) TYPE=MyISAM AUTO_INCREMENT=2 ;</p> <p>-- <br> -- Dumping data for table `members`<br> -- </p>
1
<p>INSERT INTO `members` VALUES (1, 'john', '1234');</p>

Create file main_login.php

View In Browser
|—|——————— code ——————–|—|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
<p><table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"><br> <tr><br> <form name="form1" method="post" action="checklogin.php"><br> <td><br> <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"><br> <tr><br> <td colspan="3"><strong>Member Login </strong></td><br> </tr><br> <tr><br> <td width="78">Username</td><br> <td width="6">:</td><br> <td width="294"><input name="myusername" type="text" id="myusername"></td><br> </tr><br> <tr><br> <td>Password</td><br> <td>:</td><br> <td><input name="mypassword" type="text" id="mypassword"></td><br> </tr><br> <tr><br> <td> </td><br> <td> </td><br> <td><input type="submit" name="Submit" value="Login"></td><br> </tr><br> </table><br> </td><br> </form><br> </tr><br> </table></p>
Create file checklogin.php
|—|——————— code ——————–|—|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
<p><?php<br>
$host="localhost"; // Host name <br>
$username=""; // Mysql username <br>
$password=""; // Mysql password <br>
$db_name="test"; // Database name <br>
$tbl_name="members"; // Table name </p>
<p>// Connect to server and select databse.<br>
mysql_connect("$host", "$username", "$password")or die("cannot connect"); <br>
mysql_select_db("$db_name")or die("cannot select DB");</p>
<p>// username and password sent from form <br>
$myusername=$_POST['myusername']; <br>
$mypassword=$_POST['mypassword']; </p>
<p>// To protect MySQL injection (<a href="http://us.php.net/mysql_real_escape_string" target="_blank">more detail about MySQL injection</a>)<br>
$myusername = stripslashes($myusername);<br>
$mypassword = stripslashes($mypassword);<br>
$myusername = mysql_real_escape_string($myusername);<br>
$mypassword = mysql_real_escape_string($mypassword);</p>
<p>$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";<br>
$result=mysql_query($sql);</p>
<p>// Mysql_num_row is counting table row<br>
$count=mysql_num_rows($result);<br>
// If result matched $myusername and $mypassword, table row must be 1 row</p>
<p>if($count==1){<br>
// Register $myusername, $mypassword and redirect to file "login_success.php"<br>
session_register("myusername");<br>
session_register("mypassword"); <br>
header("location:login_success.php");<br>
}<br>
else {<br>
echo "Wrong Username or Password";<br>
}<br>
?></p>
Create file login_success.php
|—|——————— code ——————–|—|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
<p>// Check if session is not registered , redirect back to main page. <br>
// Put this code in first line of web page. <br>
<? <br>
session_start();<br>
if(!session_is_registered(myusername)){<br>
header("location:main_login.php");<br>
}<br>
?><br>
<br>
<html><br>
<body><br>
Login Successful<br>
</body><br>
</html></p>
Logout.php
If you want to logout, create this file
1 2 3 4 5 6 7 8 9
// Put this code in first line of web page. <br> <? <br> session_start();<br> session_destroy();<br> ?>
For PHP5 User – checklogin.php
|—|——————— code ——————–|—|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
<?php<br>
ob_start();<br>
$host="localhost"; // Host name <br>
$username=""; // Mysql username <br>
$password=""; // Mysql password <br>
$db_name="test"; // Database name <br>
$tbl_name="members"; // Table name
<p>// Connect to server and select databse.<br>
mysql_connect("$host", "$username", "$password")or die("cannot connect"); <br>
mysql_select_db("$db_name")or die("cannot select DB");</p>
<p> // Define $myusername and $mypassword <br>
$myusername=$_POST['myusername']; <br>
$mypassword=$_POST['mypassword']; </p>
<p>// To protect MySQL injection (<a href="http://us.php.net/mysql_real_escape_string">more detail about MySQL injection</a>)<br>
$myusername = stripslashes($myusername);<br>
$mypassword = stripslashes($mypassword);<br>
$myusername = mysql_real_escape_string($myusername);<br>
$mypassword = mysql_real_escape_string($mypassword);</p>
<p>$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";<br>
$result=mysql_query($sql);</p>
<p>// Mysql_num_row is counting table row<br>
$count=mysql_num_rows($result);<br>
// If result matched $myusername and $mypassword, table row must be 1 row</p>
<p>if($count==1){<br>
// Register $myusername, $mypassword and redirect to file "login_success.php"<br>
session_register("myusername");<br>
session_register("mypassword"); <br>
header("location:login_success.php");<br>
}<br>
else {<br>
echo "Wrong Username or Password";<br>
}</p>
<p>ob_end_flush();<br>
?></p>
Encrypting Password – Make your Login More Secure
Read more about encrypting password here
July 31, 2010 at 7:42 am
Great Blog here my friend! Very informative, I appreciate all the information that you just shared with me very much and I’ll be back to read more in the future.
August 2, 2010 at 11:20 am
short and useful tutorial with informative tips. thanks
August 9, 2010 at 11:50 pm
Well I found this on Digg, and I like it so I dugg it!
August 21, 2010 at 8:48 am
Great Blog.Check Out Miniurlz To Make Your Urls Shorter
August 25, 2010 at 6:57 pm
A+ would read again
October 17, 2010 at 6:20 pm
Session_register is depreciated and should not be used anymore. You should update your script to use $_SESSION instead.