Search the Community
Showing results for tags 'chapter 11 pursue'.
-
Hi all: I am just starting to work on the pursue for chapter 11 on the register.php file.The instructions say to create a system to guarantee unique usernames in register.php. Before you attempt to create the directory, use PHP to check your list of existing usernames for a match to the just-registered name. If no match is found, thenew name is acceptable. If the username is already in use, the PHP can create an error message requesting a new username. I am needing a new way of thinking - I guess. The lines of code that I added that was not given to me, I placed in bold. I am hoping this will search the directory for the usernames listed...But this is where I get stumped... In the line of code I have written, I am asking it to search the text file for the names that were submitted - thus the scandir() function. How do I write this if statement so that it all makes sense with the rest of the code? and when the directions "before" you create your directory, do they mean that I should have placed my new code up above this line of text: $dir = '../users/'; $file = $dir . 'users.txt'; Or am I supposed to create a new directory. That really won't make sense to me, because the data is already being stored in a text file as indicated in this line: $file = $dir . 'users.txt'; <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>Register</title> <style type="text/css" media="screen"> .error { color: red; } </style> </head> <body> <h1>Register</h1> <?php // Script 11.6 - register.php /* This script registers a user by storing their information in a text file and creating a directory for them. */ // Identify the directory and file to use: $dir = '../users/'; $file = $dir . 'users.txt'; if ($_SERVER['REQUEST_METHOD'] == 'POST') { // Handle the form. $problem = FALSE; // No problems so far. // Check for each value... if (empty($_POST['username'])) { $problem = TRUE; print '<p class="error">Please enter a username!</p>'; } if (empty($_POST['password1'])) { $problem = TRUE; print '<p class="error">Please enter a password!</p>'; } if ($_POST['password1'] != $_POST['password2']) { $problem = TRUE; print '<p class="error">Your password did not match your confirmed password!</p>'; } if (!$problem) { // If there weren't any problems... if (is_writable($file)) { // Open the file. // Create the data to be written: $subdir = time() . rand(0, 4596); $data = $_POST['username'] . "\t" . md5(trim($_POST['password1'])) . "\t" . $subdir . PHP_EOL; // Write the data: file_put_contents($file, $data, FILE_APPEND | LOCK_EX); // Create the directory: mkdir ($dir . $subdir); // Print a message: print '<p>You are now registered!</p>'; } else { // Couldn't write to the file. print '<p class="error">You could not be registered due to a system error.</p>'; } } else { // Forgot a field. print '<p class="error">Please go back and try again!</p>'; } } else { // Display the form. $search_dir + '.'; $file = scandir($search_dir); foreach($contents as $item) { if } // Leave PHP and display the form: ?> <form action="register.php" method="post"> <p>Username: <input type="text" name="username" size="20" /></p> <p>Password: <input type="password" name="password1" size="20" /></p> <p>Confirm Password: <input type="password" name="password2" size="20" /></p> <input type="submit" name="submit" value="Register" /> </form> <?php } // End of submission IF. ?> </body> </html>