Advertisement
Guest User

Untitled

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