PHP Login


SUBMITTED BY: Guest

DATE: Oct. 7, 2014, 3:43 p.m.

FORMAT: Text only

SIZE: 3.7 kB

HITS: 934

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

comments powered by Disqus