Guest User

Untitled

a guest
Mar 26th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. CREATE TABLE `login` (
  2. `id` int(10) NOT NULL auto_increment,
  3. `username` varchar(20) NOT NULL,
  4. `password` varchar(20) NOT NULL,
  5. PRIMARY KEY (`id`)
  6. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
  7.  
  8. <?php
  9.  
  10. $hostname = 'localhost'; // Your MySQL hostname. Usualy named as 'localhost', so you're NOT necessary to change this even this script has already online on the internet.
  11. $dbname = ''; // Your database name.
  12. $username = ''; // Your database username.
  13. $password = ''; // Your database password. If your database has no password, leave it empty.
  14.  
  15. // Let's connect to host
  16. mysql_connect($hostname, $username, $password) or DIE('Connection to host is failed, perhaps the service is down!');
  17. // Select the database
  18. mysql_select_db($dbname) or DIE('Database name is not available!');
  19.  
  20. ?>
  21.  
  22. <?php
  23.  
  24. // Inialize session
  25. session_start();
  26.  
  27. // Check, if user is already login, then jump to secured page
  28. if (isset($_SESSION['username'])) {
  29. header('Location: securedpage.php');
  30. }
  31.  
  32. ?>
  33. <html>
  34.  
  35. <head>
  36. <title>PHPMySimpleLogin 0.3</title>
  37. </head>
  38.  
  39. <body>
  40.  
  41. <h3>User Login</h3>
  42.  
  43. <table border="0">
  44. <form method="POST" action="loginproc.php">
  45. <tr><td>Username</td><td>:</td><td><input type="text" name="username" size="20"> </td></tr>
  46. <tr><td>Password</td><td>:</td><td><input type="password" name="password" size="20"> </td></tr>
  47. <tr><td>&nbsp;</td><td>&nbsp;</td><td><input type="submit" value="Login"></td></tr>
  48. </form>
  49. </table>
  50.  
  51. </body>
  52.  
  53. </html>
  54.  
  55. <?php
  56.  
  57. // Inialize session
  58. session_start();
  59.  
  60. // Include database connection settings
  61. include('config.inc');
  62.  
  63. // Retrieve username and password from database according to user's input
  64. $login = mysql_query("SELECT * FROM user WHERE (username = '" . mysql_real_escape_string($_POST['username']) . "') and (password = '" . mysql_real_escape_string(md5($_POST['password'])) . "')");
  65.  
  66. // Check username and password match
  67. if (mysql_num_rows($login) == 1) {
  68. // Set username session variable
  69. $_SESSION['username'] = $_POST['username'];
  70. // Jump to secured page
  71. header('Location: securedpage.php');
  72. }
  73. else {
  74. // Jump to login page
  75. header('Location: index.php');
  76. }
  77.  
  78. ?>
  79.  
  80. <?php
  81.  
  82. // Inialize session
  83. session_start();
  84.  
  85. // Check, if username session is NOT set then this page will jump to login page
  86. if (!isset($_SESSION['username'])) {
  87. header('Location: index.php');
  88. }
  89.  
  90. ?>
  91. <html>
  92.  
  93. <head>
  94. <title>Secured Page</title>
  95. </head>
  96.  
  97. <body>
  98.  
  99. <p>This is secured page with session: <b><?php echo $_SESSION['username']; ?></b>
  100. <br>You can put your restricted information here.</p>
  101. <p><a href="logout.php">Logout</a></p>
  102.  
  103. </body>
  104.  
  105. </html>
  106.  
  107. <?php
  108.  
  109. // Inialize session
  110. session_start();
  111.  
  112. // Delete certain session
  113. unset($_SESSION['username']);
  114. // Delete all session variables
  115. // session_destroy();
  116.  
  117. // Jump to login page
  118. header('Location: index.php');
  119.  
  120. ?>
  121.  
  122. if (mysql_num_rows($login) == 1)
  123.  
  124. password = '" . md5(mysql_real_escape_string($_POST['password']))
  125.  
  126. ...
  127. if (!isset($_SESSION['username'])==null)
  128. {
  129. header('Location: index.php');
  130. }
  131. ...
  132.  
  133. ...
  134. if (!isset($_SESSION['username']))
  135. {
  136. header('Location: index.php');
  137. }
  138. ...
  139.  
  140. session_start();
  141. error_reporting(E_ALL ^ (E_NOTICE | E_WARNING));
  142. if(session_is_registered['username']){
  143. unset($_SESSION['username']);
  144. session_destroy($_SESSION['username']);
  145. print "<script>alert('Anda berhasil logout'); location = '/'; </script>";
  146. }
Add Comment
Please, Sign In to add comment