Advertisement
Guest User

d

a guest
Apr 26th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.15 KB | None | 0 0
  1. <?php // Script 8.4 - index.php
  2. /* This is the home page for this site.  It uses templates to create the layout. */
  3.  
  4.  
  5. // Include the header:
  6. include('templates/header.php');
  7. // Leave the PHP section to display lots of HTML:
  8. ?>
  9. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  10. <head>
  11.     <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  12.     <title>Login</title>
  13. </head>
  14. <body>
  15. <h1>Login</h1>
  16. <?php // Script 11.8 - login.php
  17. /* This script logs a user in by check the stored values in text file. */
  18.  
  19. // Identify the file to use:
  20. $file =  '../users/users.txt';
  21.  
  22. if ($_SERVER['REQUEST_METHOD'] == 'POST') { // Handle the form.
  23.  
  24.     $loggedin = FALSE; // Not currently logged in.
  25.    
  26.     // Enable auto_detect_line_settings:
  27.     ini_set('auto_detect_line_endings', 1);
  28.  
  29.     // Open the file:
  30.     $fp = fopen($file, 'rb');
  31.        
  32.     // Loop through the file:
  33.     while ( $line = fgetcsv($fp, 200, "\t") ) {
  34.  
  35.         // Check the file data against the submitted data:
  36.         if ( ($line[0] == $_POST['username']) AND ($line[1] == md5(trim($_POST['password']))) ) {
  37.        
  38.             $loggedin = TRUE; // Correct username/password combination.
  39.  
  40.             // Stop looping through the file:
  41.             break;
  42.  
  43.         } // End of IF.
  44.        
  45.     } // End of WHILE.
  46.  
  47.     fclose($fp); // Close the file.
  48.        
  49.  
  50.     // Print a message:
  51.     if ($loggedin) {
  52.         // Redirect the user to the welcome page!
  53.             $_SESSION['loggedin'] = time();
  54.                         $_SESSION['username'] = $_POST['username'];
  55.                         ob_end_clean(); // Destroy the buffer!
  56.             header ('Location: welcome.php');
  57.             exit();
  58.     } else {
  59.         print '<p style="color: red;">The username and password you entered do not match those on file.</p>';  
  60.     }
  61.        
  62. } else { // Display the form.
  63.  
  64. // Leave PHP and display the form:
  65. ?>
  66.  
  67. <form action="login.php" method="post">
  68.     <p>Username: <input type="text" name="username" size="20" /></p>
  69.     <p>Password: <input type="password" name="password" size="20" /></p>
  70.     <input type="submit" name="submit" value="Login" />
  71. </form>
  72.  
  73. <?php } // End of submission IF. ?>
  74. <?php // Return to PHP.
  75.  include('templates/footer.html'); // Include the footer.
  76. ?>
  77.  
  78. </body>
  79. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement