PHP Login script tutorial


SUBMITTED BY: Guest

DATE: Oct. 25, 2014, 12:02 p.m.

FORMAT: Text only

SIZE: 5.9 kB

HITS: 765

  1. Overview
  2. In this tutorial, we create 3 php files for testing our code.
  3. 1. main_login.php
  4. 2. checklogin.php
  5. 3. login_success.php
  6. Steps
  7. 1. Create table "members" in database "test".
  8. 2. Create file main_login.php.
  9. 3. Create file checklogin.php.
  10. 4. Create file login_success.php.
  11. 5. Create file logout.php
  12. STEP1: Create table "members"
  13. For testing this code, we need to create database "test" and create table "members"
  14. CREATE TABLE `members` (
  15. `id` int(4) NOT NULL auto_increment,
  16. `username` varchar(65) NOT NULL default '',
  17. `password` varchar(65) NOT NULL default '',
  18. PRIMARY KEY (`id`)
  19. ) TYPE=MyISAM AUTO_INCREMENT=2 ;
  20. --
  21. -- Dumping data for table `members`
  22. --
  23. INSERT INTO `members` VALUES (1, 'john', '1234');
  24. STEP2: Create file main_login.php
  25. The first file we need to create is "main_login.php" which is a login form
  26. <table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
  27. <tr>
  28. <form name="form1" method="post" action="checklogin.php">
  29. <td>
  30. <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
  31. <tr>
  32. <td colspan="3"><strong>Member Login </strong></td>
  33. </tr>
  34. <tr>
  35. <td width="78">Username</td>
  36. <td width="6">:</td>
  37. <td width="294"><input name="myusername" type="text" id="myusername"></td>
  38. </tr>
  39. <tr>
  40. <td>Password</td>
  41. <td>:</td>
  42. <td><input name="mypassword" type="text" id="mypassword"></td>
  43. </tr>
  44. <tr>
  45. <td>&nbsp;</td>
  46. <td>&nbsp;</td>
  47. <td><input type="submit" name="Submit" value="Login"></td>
  48. </tr>
  49. </table>
  50. </td>
  51. </form>
  52. </tr>
  53. </table>
  54. STEP3: Create file checklogin.php
  55. We have a login form in step 2, when a user submit their username and password, PHP code in checklogin.php will check that this user exist in our database or not.
  56. If user has the right username and password, then the code will register username and password in the session and redirect to "login_success.php". If username or password is wrong the system will show "Wrong Username or Password".
  57. ############### Code
  58. <?php
  59. $host="localhost"; // Host name
  60. $username=""; // Mysql username
  61. $password=""; // Mysql password
  62. $db_name="test"; // Database name
  63. $tbl_name="members"; // Table name
  64. // Connect to server and select databse.
  65. mysql_connect("$host", "$username", "$password")or die("cannot connect");
  66. mysql_select_db("$db_name")or die("cannot select DB");
  67. // username and password sent from form
  68. $myusername=$_POST['myusername'];
  69. $mypassword=$_POST['mypassword'];
  70. // To protect MySQL injection (more detail about MySQL injection)
  71. $myusername = stripslashes($myusername);
  72. $mypassword = stripslashes($mypassword);
  73. $myusername = mysql_real_escape_string($myusername);
  74. $mypassword = mysql_real_escape_string($mypassword);
  75. $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
  76. $result=mysql_query($sql);
  77. // Mysql_num_row is counting table row
  78. $count=mysql_num_rows($result);
  79. // If result matched $myusername and $mypassword, table row must be 1 row
  80. if($count==1){
  81. // Register $myusername, $mypassword and redirect to file "login_success.php"
  82. session_register("myusername");
  83. session_register("mypassword");
  84. header("location:login_success.php");
  85. }
  86. else {
  87. echo "Wrong Username or Password";
  88. }
  89. ?>
  90. STEP4: Create file login_success.php
  91. User can't view this page if the session is not registered.
  92. ############### Code
  93. // Check if session is not registered, redirect back to main page.
  94. // Put this code in first line of web page.
  95. <?php
  96. session_start();
  97. if(!session_is_registered(myusername)){
  98. header("location:main_login.php");
  99. }
  100. ?>
  101. <html>
  102. <body>
  103. Login Successful
  104. </body>
  105. </html>
  106. STEP5: Create file Logout.php
  107. If you want to logout, create this file. The code in this file will destroy the session.
  108. // Put this code in first line of web page.
  109. <?php
  110. session_start();
  111. session_destroy();
  112. ?>
  113. For PHP5 User - checklogin.php
  114. ############### Code
  115. <?php
  116. ob_start();
  117. $host="localhost"; // Host name
  118. $username=""; // Mysql username
  119. $password=""; // Mysql password
  120. $db_name="test"; // Database name
  121. $tbl_name="members"; // Table name
  122. // Connect to server and select databse.
  123. mysql_connect("$host", "$username", "$password")or die("cannot connect");
  124. mysql_select_db("$db_name")or die("cannot select DB");
  125. // Define $myusername and $mypassword
  126. $myusername=$_POST['myusername'];
  127. $mypassword=$_POST['mypassword'];
  128. // To protect MySQL injection (more detail about MySQL injection)
  129. $myusername = stripslashes($myusername);
  130. $mypassword = stripslashes($mypassword);
  131. $myusername = mysql_real_escape_string($myusername);
  132. $mypassword = mysql_real_escape_string($mypassword);
  133. $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
  134. $result=mysql_query($sql);
  135. // Mysql_num_row is counting table row
  136. $count=mysql_num_rows($result);
  137. // If result matched $myusername and $mypassword, table row must be 1 row
  138. if($count==1){
  139. // Register $myusername, $mypassword and redirect to file "login_success.php"
  140. session_register("myusername");
  141. session_register("mypassword");
  142. header("location:login_success.php");
  143. }
  144. else {
  145. echo "Wrong Username or Password";
  146. }
  147. ob_end_flush();
  148. ?>
  149. Encrypting Password - Make your Login More Secure

comments powered by Disqus