Jump to content
Larry Ullman's Book Forums

AprilSwenby

Members
  • Posts

    85
  • Joined

  • Last visited

Everything posted by AprilSwenby

  1. This is what I am finding on line and I am thinking I should put this right after my line item that checks if the username has been submitted: However, this line of code is checking against a database... and I haven't learned that yet, so it's pretty unfamliar code to me. I have no idea what !get_magic_quotes_gpc means! Basically though - I think I need to write something like this only I need to refer it to the text file. Again - if you remember, I can fluently read PHP, but I am just barely learning to write it - so I am struggling.... I am getting lots of practice by taking scripts and modifying them.... /* check they filled in what they supposed to, passwords matched, username isn't already taken, etc. */ if(!$_POST['uname'] | !$_POST['passwd'] | !$_POST['passwd_again'] | !$_POST['email']) { die('You didn\'t fill in a required field.'); } // check if username exists in database. if(!get_magic_quotes_gpc()) { $_POST['uname'] = addslashes($_POST['uname']); } $name_check = $db_object->query("SELECT username FROM users WHERE username = '".$_POST['uname']."'"); if(DB::isError($name_check)) { die($name_check->getMessage()); } $name_checkk = $name_check->numRows(); if($name_checkk != 0) { die('Sorry, the username: '.$_POST['uname'].' is already taken, please pick another one.');
  2. My text file is blank - until a user fills out the form - then whatever they plugged in for the user name and password - will get put in the file.
  3. The form inputs are validated.... I can find all sorts of code examples on the internet about how to match the input data with what is stored on the database, but nothing on how to write the code to match it to what is stored in the text field. Based on your last comment, as best as I can comprehend right now, I need to put something like this in... My problem is - I know this syntax isn't correct and I don't know where in the script I should place this. //check the directory for duplicate names $file = scandir($file); // if the number of rows returned = 0 then the name has not been used and it available. if (username($name) >= 1) { // already in use? $error[] ='This name is already in use please try another name.'; }else{ //email is alright and no error
  4. The text file will contain both user names and text files. Here is something i was toying with- but of course... it's not working.... I have tried multiple others too... $count_user = "Select count(*) from profiles where username = '$username'"; if (don't know what function to put here!($count_user, 0, "count(*)") != 0) { $problem = TRUE; print '<p class="error">Your name has already been chosen, please choose a different name</p>'; // There's someone else with this. } else {
  5. Perhaps I've gone about this all wrong - maybe the code needs to be added in between write the data and the create the directory? Take a look at my theory below: What are you thoughts? Do you think I may be onto something? If so - How can I put My "english" into "php" // Write the data: file_put_contents($file, $data, FILE_APPEND | LOCK_EX); //Check to see if the name field has been entered in the text file yet // if statement If it has been then produce and error message if it hasn't then continue onto creating the directory // Create the directory: mkdir ($dir . $subdir);
  6. Hi Jonathon - we meet again! I agree that what you have said is the idea. The form is already validated that the information is entered.... Where do I start to make sure that what is entered for the name hasn't already been added to the text file?
  7. 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>
  8. Well - here's what I've got! I actually figured out how to make the form - but I need some help determining my next steps to making the form interact with my functions... <?php //Script 10.1 Menus.php //make the new form that will input user selection function make_form_input($name, $label) { // Begin a paragraph and a label: print '<p><label>' . $label . ': '; // Begin the input: print '<input type="text" name="' . $name . '" '; // Add the value: if (isset($_POST[$name])) { print ' value="' . htmlspecialchars($_POST[$name]) . '"'; } // Complete the input, the label and the paragraph: print ' /></label></p>'; } // End of make_text_input() function. // Make the form: print '<form action="" method="post">'; // Create some text inputs: make_form_input('first_year', 'Choose the first year in the menu'); make_form_input('year_count', 'How many years would you like it to generate?'); print '<input type="submit" name="submit" value="Submit!" /></form>'; print '<br />'; //Array to store the months: index starting at 1 to make sure January is starting at 1 not 0 function make_date_menus(){ $months=array (1=>'January','February','March','April','May','June','July','August','September','October','November','December'); // Make the month pull-down menu: print'<select name="month">'; foreach ($months as $key => $value){ print"\n<option value=\"$key\">$value</option>"; } print"</select>"; // Make the day pull-down menu: print'<select name="day">'; for($day =1; $day<=31; $day++){ print"\n<option value=\"$day\">$day</option>"; } print"</select>"; // Make the year pull-down menu: print'<select name="year">'; $start_year = date('Y'); for($y=$start_year; $y<=($start_year +10); $y++){ print"\n<option value=\"$y\">$y</option>"; } print'</select>'; }// End of make_date_menus() function. // Make the form: print'<form action="" method="post">'; make_date_menus(); print'</form>'; ?>
  9. Thank you for clarifying the directions - I understand now what he is looking for... I have no idea yet on how to make that happen - but I imagine I'll be up all night trying to figure it out! Thanks! April
  10. Bear with me here, but when you say arguments, my mind goes back to line item number 13 on page 261 and see that their aren't arguments in the specific function make_date_menus - so when you say "change the arguments" what line in the code are you referring to? Will the end result even be different, because as it stands in the book and when I run the code as the book states, I can choose the year I want... So what is he asking us to change? <?php //Script 10.1 Menus.php //Array to store the months: index starting at 1 to make sure January is starting at 1 not 0 function make_date_menus(){ $months=array (1=>'January','February','March','April','May','June','July','August','September','October','November','December'); // Make the month pull-down menu: print'<select name="month">'; foreach ($months as $key => $value){ print"\n<option value=\"$key\">$value</option>"; } print"</select>"; // Make the day pull-down menu: print'<select name="day">'; for($day =1; $day<=31; $day++){ print"\n<option value=\"$day\">$day</option>"; } print"</select>"; // Make the year pull-down menu: print'<select name="year">'; $start_year = date('Y'); for($y=$start_year; $y<=($start_year +10); $y++){ print"\n<option value=\"$y\">$y</option>"; } print'</select>'; }// End of make_date_menus() function. // Make the form: print'<form action="" method="post">'; make_date_menus(); print'</form>'; ?>
  11. I am not sure...The instructions say Rewrite the make_text_input() function so that it can be told whether to look for an existing value in either $_POST or $_GET. This one seemed to come to easy for me so I was thinking it wasn't right.... By the way - I really, really appreciate your fast prompt help - you should be a teacher This stuff seems to come so easy for you! I am struggling my way through this book - and I am feeling like I need to be babysat step by step.... I can read it, but I can't write it!
  12. Hi Larry. Can you clarify exactly what you are asking of us in the Pursue Question Chapter 10 # 1? I don't understand what you are asking it to do? Can you give me some steps to help me along the way?
  13. Are you ready to help me with my next dilemma? I am to rewrite teh make_text_input() function so that it can be told whehte rto look for an existing value in either $_POST or $_GET. I think I've done this right... Have I? This is my code... <?php // pursue # 2 /* This script defines and calls a function that creates a sticky text input and can be told whether to look for an existing value in either _$POST or _$GET */ // This function makes a sticky text input. // This function requires three arguments be passed to it. function make_text_input($name, $label) { // Begin a paragraph and a label: print '<p><label>' . $label . ': '; // Begin the input: print '<input type="text" name="' . $name . '" size="20" '; // Add the value: if (isset($_POST[$name])) { print ' value="' . htmlspecialchars($_POST[$name]) . '"'; } else { if (isset($_GET[$name])) { print ' value="' . htmlspecialchars($_GET[$name]) . '"'; } } // Complete the input, the label and the paragraph: print ' /></label></p>'; } // End of make_text_input() function. // Make the form: print '<form action="" method="post">'; // Create some text inputs: make_text_input('first_name', 'First Name'); make_text_input('last_name', 'Last Name'); make_text_input('email', 'Email Address'); print '<input type="submit" name="submit" value="Register!" /></form>'; ?>
  14. Jonathon!!! Is this it!!!??? <?php // Script 10.2 - sticky1.php /* This script defines and calls a function that creates a sticky text input. */ // This function makes a sticky text input. // This function requires three arguments be passed to it. function make_text_input($name, $label, $password) { // Begin a paragraph and a label: print '<p><label>' . $label . ': '; print '<input type="' . $password . '" name="' . $name . '" size="20" '; // Add the value: if (isset($_POST[$name])) { print ' value="' . htmlspecialchars($_POST[$name]) . '"'; } else { if (isset($_POST[$password])) { print ' value="' . htmlspecialchars($_POST[$password]) . '"'; } } // Complete the input, the label and the paragraph: print ' /></label></p>'; } // End of make_text_input() function. // Make the form: print '<form action="" method="post">'; // Create some text inputs: make_text_input('first_name', 'First Name', 'text'); make_text_input('last_name', 'Last Name', 'text'); make_text_input('email', 'Email Address','text'); make_text_input ('password', 'Enter your password', 'password'); print '<input type="submit" name="submit" value="Register!" /></form>'; ?>
  15. I think I understand what the source code should look like... That's how I know I haven't met the requirements.... From my last two examples - do you see what I am trying to do to get that password field to read a different input?
  16. AND I tried this: <?php // Script 10.2 - sticky1.php /* This script defines and calls a function that creates a sticky text input. */ // This function makes a sticky text input. // This function requires three arguments be passed to it. function make_text_input($name, $label, $password) { // Begin a paragraph and a label: print '<p><label>' . $label . ': '; // Add the value: if (isset($_POST[$name])) { print ' value="' . htmlspecialchars($_POST[$name]) . '"'; } else { if (isset($_POST[$password])) { print ' value="' . htmlspecialchars($_POST[$password]) . '"'; } } // Complete the input, the label and the paragraph: print ' /></label></p>'; } // End of make_text_input() function. // Make the form: print '<form action="" method="post">'; // Create some text inputs: make_text_input('first_name', 'First Name', '<input type="text" name="fname" size="20"/>'); make_text_input('last_name', 'Last Name', '<input type="text" name="lname" size="20"/>'); make_text_input('email', 'Email Address','<input type="text" name="email" size="20"/>'); make_text_input ('password', 'Enter your password', '<input type="password" name="password" size="20"/>'); print '<input type="submit" name="submit" value="Register!" /></form>'; ?>
  17. By the way - I'm actually trying things throughout our conversations - and this is my latest, which didn't give me errors but my source doesn't look the way you want it to: // Create some text inputs: make_text_input('first_name', 'First Name', NULL); make_text_input('last_name', 'Last Name', NULL); make_text_input('email', 'Email Address', NULL); make_text_input ('password', 'Enter your password', '<input type="password" />');
  18. Maybe if you could show me what the input for just the name should look like - I'm having trouble differentiating at how these custom functions should look when they are doing the same thing. By the way - I appreciate you not just "giving me the answer" I am learning as I go....
  19. Jonathon - here is what I have.... From what I can see.... I did that. <?php // Script 10.2 - sticky1.php /* This script defines and calls a function that creates a sticky text input. */ // This function makes a sticky text input. // This function requires three arguments be passed to it. function make_text_input($name, $label, $password) { // Begin a paragraph and a label: print '<p><label>' . $label . ': '; // Begin the input: print '<input type="text" name="' . $name . '" size="20" '; print '<input type="password" name="' . $password . '" size="20" '; // Add the value: if (isset($_POST[$name])) { print ' value="' . htmlspecialchars($_POST[$name]) . '"'; } else { if (isset($_POST[$password])) { print ' value="' . htmlspecialchars($_POST[$password]) . '"'; } } // Complete the input, the label and the paragraph: print ' /></label></p>'; } // End of make_text_input() function. // Make the form: print '<form action="" method="post">'; // Create some text inputs: make_text_input('first_name', 'First Name', NULL); make_text_input('last_name', 'Last Name', NULL); make_text_input('email', 'Email Address', NULL); make_text_input ('password', 'Enter your password', '$password'); print '<input type="submit" name="submit" value="Register!" /></form>'; ?> If I add two lines: print '<input type="text" name="' . $name . '" size="20" '; print '<input type="password" name="' . $password . '" size="20" '; I get two input boxes, but the second line goes with my make_text_input ('password', 'Enter your password', '$password'); line. I just don't understand why this isn't working. I have the argument $password and I am referring to it in my make_text_input function and I am also clarifying the input type that the $password variable should be....
  20. you probably think I'm a real nut job here... LOL But don't get me wrong - I really appreciate your help! When you say "invoke Functions here" Do you mean that I have to create an if statement or something there? I've been googling this - can't I just change my text input to "hidden" or "password"? But then I suppose it will change ALL of my input boxes.... I'm stuck!
  21. Yes, I follow - but I don't know how to make the 3 fields prior to that take "text inputs" and the password field take just "password" inputs. If I add a second line that looks like this: print '<input type="text" name="' . $name . '" size="20" '; print '<input type="password" name="' . $password . '" size="20" '; I get two input boxes.... That's not what I want, but it does give me the code source that you are referring to for the second box. I need to figure out how to make just the password box be an input type of "password" I don't know what to or what to change to make that happen.
  22. I am really sorry that - but I just am not connecting here... So you think I should change this line? print '<input type="text" name="' . $name . '" size="20" '; or add another line like it that will print input types of "password"?
  23. um - I think I'm getting a little excited here Am I onto something with this script? // This function makes a sticky text input. // This function requires three arguments be passed to it. function make_text_input($name, $label, $password) { // Begin a paragraph and a label: print '<p><label>' . $label . ': '; // Begin the input: print '<input type="text" name="' . $name . '" size="20" '; // Add the value: if (isset($_POST[$name])) { print ' value="' . htmlspecialchars($_POST[$name]) . '"'; } else { if (isset($_POST[$password])) { print ' value="' . htmlspecialchars($_POST[$password]) . '"'; } } // Complete the input, the label and the paragraph: print ' /></label></p>'; } // End of make_text_input() function. // Make the form: print '<form action="" method="post">'; // Create some text inputs: make_text_input('first_name', 'First Name', NULL); make_text_input('last_name', 'Last Name', NULL); make_text_input('email', 'Email Address', NULL); make_text_input ('password', 'Enter your password', '$type'); print '<input type="submit" name="submit" value="Register!" /></form>'; ?>
  24. Thanks Jonathon! But what do you mean "tweak your function" Can you tell me which line item you are referring to? I have looked in the book - and I know you don't have it - but I cannot find anything specific for that... I"m sure it's there, I just don't know what I am looking for. Are you referring to the if statement or the user defined function itself? What should I be looking at doing to those lines?
×
×
  • Create New...