PHP Login Script Tutorial

Posted July 30th, 2010 by

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

Step1:

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>

db members PHP Login Script Tutorial

Step2:

Create file main_login.php

member login PHP Login Script Tutorial
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>

Step3:

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>

Step4:

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>

Step5:

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

Step6:

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>

Step7:

Encrypting Password – Make your Login More Secure

encrypt password PHP Login Script Tutorial

Read more about encrypting password here



Random Posts

Hello there! If you are new here, you might want to subscribe to the RSS feed for updates on this topic, or follow us on Twitter. Get promotion with Hypesol.
Name: Email:
6 ResponsesLeave a comment
  • Jhon Mary
    July 31, 2010 at 7:42 am
    Reply

    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.

  • Tutorial Lounge
    August 2, 2010 at 11:20 am
    Reply

    short and useful tutorial with informative tips. thanks

  • tinnitis
    August 9, 2010 at 11:50 pm
    Reply

    Well I found this on Digg, and I like it so I dugg it!

  • Reinaldo Rodriguel
    August 21, 2010 at 8:48 am
    Reply

    Great Blog.Check Out Miniurlz To Make Your Urls Shorter

  • mode20100
    August 25, 2010 at 6:57 pm
    Reply

    A+ would read again

  • deranjer
    October 17, 2010 at 6:20 pm
    Reply

    Session_register is depreciated and should not be used anymore. You should update your script to use $_SESSION instead.

Add a commentGet a Gravatar

* Name

* Email Address

Website Address

You can usethese tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>



  •  
  •  
  •  
Around The Site
Free Subscription

Enter your email address:

 
Become Facebook Fan
Links
Get Adobe Flash playerPlugin by wpburn.com wordpress themes