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>Register</title>
7.		<style type="text/css" media="screen">
8.			.error { color: red; }
9.		</style>
10.	</head>
11.	<body>
12.	<?php // Script 11.6 - register.php
13.	/* This script registers a user by storing their information in a text file and creating a directory for them. */
14.	
15.	if (isset($_POST['submitted'])) { // Handle the form.
16.	
17.		$problem = FALSE; // No problems so far.
18.		
19.		// Check for each value...
20.		if (empty($_POST['username'])) {
21.			$problem = TRUE;
22.			print '<p class="error">Please enter a username!</p>';
23.		}	
24.	
25.		if (empty($_POST['password1'])) {
26.			$problem = TRUE;
27.			print '<p class="error">Please enter a password!</p>';
28.		}
29.		
30.		if ($_POST['password1'] != $_POST['password2']) {
31.			$problem = TRUE;
32.			print '<p class="error">Your password did not match your confirmed password!</p>';
33.		} 
34.		
35.		if (!$problem) { // If there weren't any problems...
36.		
37.			if ($fp = fopen ('../users/users.txt', 'ab')) { // Open the file.
38.				
39.				// Set the encoding:
40.				stream_encoding($fp, 'utf-8');
41.	
42.				// Create the data to be written:
43.				$dir = time() . rand(0, 4596);
44.				$data = $_POST['username'] . "\t" . md5(trim($_POST['password1'])) . "\t" . $dir . "\n"; // \r\n on Windows
45.				
46.				// Write the data and close the file:
47.				fwrite ($fp, $data);
48.				fclose ($fp);
49.				
50.				// Create the directory:
51.				mkdir ("../users/$dir");
52.	
53.				// Print a message:
54.				print '<p>You are now registered!</p>';
55.			
56.			} else { // Couldn't write to the file.
57.				print '<p class="error">You could not be registered due to a system error.</p>';
58.			}
59.			
60.		} else { // Forgot a field.
61.			print '<p class="error">Please go back and try again!</p>';	
62.		}
63.	
64.	} else { // Display the form.
65.	
66.	// Leave PHP and display the form:
67.	?>
68.	
69.	<form action="register.php" method="post">
70.		<p>Username: <input type="text" name="username" size="20" /></p>
71.		<p>Password: <input type="password" name="password1" size="20" /></p>
72.		<p>Confirm Password: <input type="password" name="password2" size="20" /></p><br />
73.		<input type="submit" name="submit" value="Register" />
74.		<input type="hidden" name="submitted" value="true" />
75.	</form>
76.	<?php } // End of submission IF. ?>
77.	</body>
78.	</html>
79.