1. <?php // Script 9.5 - login.php #3
2. /* This page lets people log into the site (almost!). */
3.
4. // Set the page title and include the header file:
5. define('TITLE', 'Login');
6. require('templates/header.html');
7.
8. // Print some introductory text:
9. print '<h1>Login Form</h1>
10. <p>Users who are logged in can take advantage of certain features like this, that, and the other thing.</p>';
11.
12. // Check if the form has been submitted:
13. if ( isset($_POST['submitted']) ) {
14.
15. // Handle the form:
16. if ( (!empty($_POST['email'])) && (!empty($_POST['password'])) ) {
17.
18. if ( (strtolower($_POST['email']) == 'me@example.com') && ($_POST['password'] == 'testpass') ) { // Correct!
19.
20. // Do session stuff:
21. session_start();
22. $_SESSION['email'] = $_POST['email'];
23. $_SESSION['loggedin'] = time();
24.
25. // Redirect the user to the welcome page!
26. ob_end_clean(); // Destroy the buffer!
27. header ('Location: welcome.php');
28. exit();
29.
30. } else { // Incorrect!
31.
32. print '<p>The submitted email address and password do not match those on file!<br />Go back and try again.</p>';
33.
34. }
35.
36. } else { // Forgot a field.
37.
38. print '<p>Please make sure you enter both an email address and a password!<br />Go back and try again.</p>';
39.
40. }
41.
42. } else { // Display the form.
43.
44. print '<form action="login.php" method="post">
45. <p>Email Address: <input type="text" name="email" size="20" /></p>
46. <p>Password: <input type="password" name="password" size="20" /></p>
47. <p><input type="submit" name="submit" value="Log In!" /></p>
48. <input type="hidden" name="submitted" value="true" />
49. </form>';
50.
51. }
52.
53. require('templates/footer.html'); // Need the footer.
54. ?>