· 10 years ago · Apr 22, 2016, 04:33 AM
1THE INDEX
2
3<?php
4//Create Session
5session_start();
6//header
7include('./includes/header.html');
8
9//If a user name is entered display login mesage
10 if (isset($_SESSION['first_name'])) {
11 echo "You currently logged in as {$_SESSION['first_name']}. Welcome to our website!";
12}
13
14FOOTER
15//header
16include('./includes/footer.html');
17?>
18<!-- Script 3.3 - footer.html -->
19 <!-- End of the page-specific content. --></div>
20
21 <div id="footer">
22 <p>Copyright © <a href="#">Plain and Simple</a> 2007 | Designed by <a href="http://www.edg3.co.uk/">edg3.co.uk</a> | Sponsored by <a href="http://www.opendesigns.org/">Open Designs</a> | Valid <a href="http://jigsaw.w3.org/css-validator/">CSS</a> & <a href="http://validator.w3.org/">XHTML</a></p>
23 </div>
24</body>
25</html>
26
27HEADER
28
29<?php session_start(); ?>
30<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
31<html xmlns="http://www.w3.org/1999/xhtml">
32<head">
33 <title><?php echo $page_title; ?></title>
34 <link rel="stylesheet" href="includes/style.css" type="text/css" media="screen" />
35 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
36</head>
37<body>
38 <div id="header">
39 <h1>Your Website</h1>
40 <h2>catchy slogan...</h2>
41 </div>
42 <div id="navigation">
43 <ul>
44 <li><a href="index.php">Home Page</a></li>
45 <li><a href="register.php">Register</a></li>
46 <li><a href="view_users.php">View Users</a></li>
47 <li><a href="password.php">Change Password</a></li>
48 <li><?php // Create a login/logout link:
49if ( (isset($_SESSION['user_id'])) && (basename($_SERVER['PHP_SELF']) != 'logout.php') ) {
50 echo '<a href="logout.php">Logout</a>';
51} else {
52 echo '<a href="login.php">Login</a>';
53}
54?></li>
55 </ul>
56 </div>
57 <div id="content"><!-- Start of the page-specific content. -->
58<!-- Script 12.7 - header.html -->
59
60
61
62
63
64
65HERE BE THE QUERIES ---- HOW MANY TABLES? LOOKS LIKE JUST USERS am I missing a column?
66
67POST TEST
68
69<?php
70session_start(); // Start the session.
71if (isset($_SESSION['user_id'])) {
72 echo 'You are logged in and can post on this page!
73 <br />
74 <form>
75 <textarea name="comment" cols="40" rows="5">
76 </textarea>
77 <br /><br />
78 <input type="submit" name="submit" value="Submit" />
79 </form>';
80} else {
81 header('Location: http://dwlehman.uwmsois.com/assignment10/login.php');
82}
83?>
84
85REGISTER
86
87<?php # Script 9.5 - register.php #2
88// This script performs an INSERT query to add a record to the users table.
89
90$page_title = 'Register';
91include ('includes/header.html');
92
93// Check for form submission:
94if ($_SERVER['REQUEST_METHOD'] == 'POST') {
95
96 require ('./includes/mysqli_connect.php'); // Connect to the db.
97
98 $errors = array(); // Initialize an error array.
99
100 // Check for a first name:
101 if (empty($_POST['first_name'])) {
102 $errors[] = 'You forgot to enter your first name.';
103 } else {
104 $fn = mysqli_real_escape_string($dbc, trim($_POST['first_name']));
105 }
106
107 // Check for a last name:
108 if (empty($_POST['last_name'])) {
109 $errors[] = 'You forgot to enter your last name.';
110 } else {
111 $ln = mysqli_real_escape_string($dbc, trim($_POST['last_name']));
112 }
113
114 // Check for an email address:
115 if (empty($_POST['email'])) {
116 $errors[] = 'You forgot to enter your email address.';
117 } else {
118 $e = mysqli_real_escape_string($dbc, trim($_POST['email']));
119 }
120
121 // Check for a password and match against the confirmed password:
122 if (!empty($_POST['pass1'])) {
123 if ($_POST['pass1'] != $_POST['pass2']) {
124 $errors[] = 'Your password did not match the confirmed password.';
125 } else {
126 $p = mysqli_real_escape_string($dbc, trim($_POST['pass1']));
127 }
128 } else {
129 $errors[] = 'You forgot to enter your password.';
130 }
131
132 if (empty($errors)) { // If everything's OK.
133
134 // Register the user in the database...
135
136 // Make the query:
137 $q = "INSERT INTO users (first_name, last_name, email, pass, registration_date) VALUES ('$fn', '$ln', '$e', SHA1('$p'), NOW() )";
138 $r = @mysqli_query ($dbc, $q); // Run the query.
139 if ($r) { // If it ran OK.
140
141 // Print a message:
142 echo '<h1>Thank you!</h1>
143 <p>You are now registered. In Chapter 12 you will actually be able to log in!</p><p><br /></p>';
144
145 } else { // If it did not run OK.
146
147 // Public message:
148 echo '<h1>System Error</h1>
149 <p class="error">You could not be registered due to a system error. We apologize for any inconvenience.</p>';
150
151 // Debugging message:
152 echo '<p>' . mysqli_error($dbc) . '<br /><br />Query: ' . $q . '</p>';
153
154 } // End of if ($r) IF.
155
156 mysqli_close($dbc); // Close the database connection.
157
158 // Include the footer and quit the script:
159 include ('includes/footer.html');
160 exit();
161
162 } else { // Report the errors.
163
164 echo '<h1>Error!</h1>
165 <p class="error">The following error(s) occurred:<br />';
166 foreach ($errors as $msg) { // Print each error.
167 echo " - $msg<br />\n";
168 }
169 echo '</p><p>Please try again.</p><p><br /></p>';
170
171 } // End of if (empty($errors)) IF.
172
173 mysqli_close($dbc); // Close the database connection.
174
175} // End of the main Submit conditional.
176?>
177<h1>Register</h1>
178<form action="register.php" method="post">
179 <p>First Name: <input type="text" name="first_name" size="15" maxlength="20" value="<?php if (isset($_POST['first_name'])) echo $_POST['first_name']; ?>" /></p>
180 <p>Last Name: <input type="text" name="last_name" size="15" maxlength="40" value="<?php if (isset($_POST['last_name'])) echo $_POST['last_name']; ?>" /></p>
181 <p>Email Address: <input type="text" name="email" size="20" maxlength="60" value="<?php if (isset($_POST['email'])) echo $_POST['email']; ?>" /> </p>
182 <p>Password: <input type="password" name="pass1" size="10" maxlength="20" value="<?php if (isset($_POST['pass1'])) echo $_POST['pass1']; ?>" /></p>
183 <p>Confirm Password: <input type="password" name="pass2" size="10" maxlength="20" value="<?php if (isset($_POST['pass2'])) echo $_POST['pass2']; ?>" /></p>
184 <p><input type="submit" name="submit" value="Register" /></p>
185</form>
186<?php include ('includes/footer.html'); ?>
187LOGIN
188
189?php # Script 12.12 - login.php #4
190// This page processes the login form submission.
191// The script now stores the HTTP_USER_AGENT value for added security.
192
193// Check if the form has been submitted:
194if ($_SERVER['REQUEST_METHOD'] == 'POST') {
195
196 // Need two helper files:
197 require ('includes/login_functions.inc.php');
198 require ('./includes/mysqli_connect.php');
199
200 // Check the login:
201 list ($check, $data) = check_login($dbc, $_POST['email'], $_POST['pass']);
202
203 if ($check) { // OK!
204
205 // Set the session data:
206 session_start();
207 $_SESSION['user_id'] = $data['user_id'];
208 $_SESSION['first_name'] = $data['first_name'];
209
210 // Store the HTTP_USER_AGENT:
211 $_SESSION['agent'] = md5($_SERVER['HTTP_USER_AGENT']);
212
213 // Redirect:
214 redirect_user('loggedin.php');
215
216 } else { // Unsuccessful!
217
218 // Assign $data to $errors for login_page.inc.php:
219 $errors = $data;
220
221 }
222
223 mysqli_close($dbc); // Close the database connection.
224
225} // End of the main submit conditional.
226
227// Create the page:
228include ('includes/login_page.inc.php');
229?>
230
231LOGOUT
232
233<?php # Script 12.11 - logout.php #2
234// This page lets the user logout.
235// This version uses sessions.
236
237session_start(); // Access the existing session.
238
239// If no session variable exists, redirect the user:
240if (!isset($_SESSION['user_id'])) {
241
242 // Need the functions:
243 require ('includes/login_functions.inc.php');
244 redirect_user();
245
246} else { // Cancel the session:
247
248 $_SESSION = array(); // Clear the variables.
249 session_destroy(); // Destroy the session itself.
250 setcookie ('PHPSESSID', '', time()-3600, '/', '', 0, 0); // Destroy the cookie.
251
252}
253
254// Set the page title and include the HTML header:
255$page_title = 'Logged Out!';
256include ('includes/header.html');
257
258// Print a customized message:
259echo "<h1>Logged Out!</h1>
260<p>You are now logged out!</p>";
261
262include ('includes/footer.html');
263?>