1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
4. <head>
5. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
6. <title>Login</title>
7. </head>
8. <body>
9. <?php // Script 11.7 - login.php
10. /* This script logs a user in by check the stored values in text file. */
11.
12. if (isset($_POST['submitted'])) { // Handle the form.
13.
14. $loggedin = FALSE; // Not currently logged in.
15.
16. // Enable auto_detect_line_settings:
17. ini_set('auto_detect_line_endings', 1);
18.
19. // Open the file:
20. $fp = fopen('../users/users.txt', 'rb');
21.
22. // Loop through the file:
23. while ( $line = fgetcsv($fp, 100, "\t") ) {
24.
25. // Check the file data against the submitted data:
26. if ( ($line[0] == $_POST['username']) AND ($line[1] == md5(trim($_POST['password']))) ) {
27.
28. $loggedin = TRUE; // Correct username/password combination.
29.
30. // Stop looping through the file:
31. break;
32.
33. } // End of IF.
34.
35. } // End of WHILE.
36.
37. fclose($fp); // Close the file.
38.
39. // Print a message:
40. if ($loggedin) {
41. print '<p>You are now logged in.</p>';
42. } else {
43. print '<p style="color: red;">The username and password you entered do not match those on file.</p>';
44. }
45.
46. } else { // Display the form.
47.
48. // Leave PHP and display the form:
49. ?>
50.
51. <form action="login.php" method="post">
52. <p>Username: <input type="text" name="username" size="20" /></p>
53. <p>Password: <input type="password" name="password1" size="20" /></p>
54. <input type="submit" name="submit" value="Login" />
55. <input type="hidden" name="submitted" value="true" />
56. </form>
57. <?php } // End of submission IF. ?>
58. </body>
59. </html>
60.