Jump to content
Larry Ullman's Book Forums


  • Posts

  • Joined

  • Last visited

  • Days Won


Everything posted by phpRob

  1. In Chapter 11 after the user has successfully registered mkdir is called to create a directory in the 'users' directory. I think I may have missed the point of why this is done, could you please explain Larry, I'm a little confused? As it stands every time the user registers that record is recorded in the users.txt file and an empty directory is also created within the 'users' directory.
  2. This is the code I used, I didn't read this post at all and thought 'd have a stab at it myself! It seems to work but as always are there any areas I coud improve on Larry? By the way I'm also learning from the book and I'm at the exact same position of the book as you April. Like Larry said it is smetimes best to step away from the computer and just think about the logic in plain english, I did this with this exercise after getting myself into a pickle and finally worked it out (I think!) <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>Register</title> </head> <style> .error { color: red; } </style> <body> <?php //Identify directory and file to write to $dir = '../users/'; $file = $dir . 'users.txt'; //Set flag $problem = FALSE; //Handle form if ( ($_SERVER['REQUEST_METHOD'] == 'POST') ) { //Validate Username if ( empty($_POST['username']) ) { print '<p class="error">Please enter a username</p>'; $problem = TRUE; } //Validate Password if ( empty($_POST['password']) ) { print '<p class="error">Please enter a password</p>'; $problem = TRUE; } //Validate Password if ( ($_POST['password'] != $_POST['password1']) ) { print '<p class="error">Please make sure passwords match</p>'; $problem = TRUE; } ini_set('auto_detect_line_endings', 1); //Open file $fp = fopen($file, 'rb'); //Read file while ( $line = fgetcsv($fp, 200, "\t") ) { //Check for username pairs if ( $line[0] == $_POST['username'] ) { $problem = TRUE; print '<p>Username already exists, please choose another username</p>'; break; } } fclose($fp); //If fields are valid if ( !$problem ) { //Is file writable if ( is_writable($file)) { //Create the data $subdir = time() . rand(0, 4999); $data = $_POST['username'] . "\t" . md5(trim($_POST['password'])) . "\t" . $subdir . PHP_EOL; //Write the data file_put_contents($file, $data, FILE_APPEND | LOCK_EX ); //Create Directory mkdir($dir . $subdir); print '<p>Thankyou, you have been successfully registered</p>'; } else { print '<p class="error">Error writing to file</p>'; } } else { print '<p class="error">Please enter a valid username and password</p>'; } } else { //Display form ?> <form action="register.php" method="post"> <p>Username: <input type="text" name="username" size="20" /></p> <p>Password: <input type="password" name="password" size="20" /></p> <p>Confirm password: <input type="password" name="password1" size="20" /></p> <p><input type="submit" name="submit" value="Register" /></p> </form> <?php } //End of IF statement ?> </body> </html>
  3. This attribute is optional, if left blank it will use the document address. I guess it is good prcatice to use the script name in the action attribute even though it's used on the same page. I'm just following Larry's code so he'd be the best one to answer this question. You do make a valid point though.
  4. I too had this problem, I entered in my name and email and never received a confirmation email? I would still like to subscribe to your newsletter, so I am registered? Thanks
  5. Would this be correct for the pursue question #3 <body> <?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 two arguments be passed to it. function make_text_input($name, $label, $type, $size = 20) { // Begin a paragraph and a label: print '<p><label>' . $label . ': '; print '<input type="'. $type . '" name="' . $name . '" size="' . $size . '"'; // 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_text_input('first_name', 'First Name', 'text', ''); make_text_input('last_name', 'Last Name', 'text', '50'); make_text_input('email', 'Email Address', 'text', '50'); make_text_input('password', 'Password', 'password', ''); print '<input type="submit" name="submit" value="Register!" /></form>'; ?> </body>
  6. To answer this question, it really is quite simple although I did take quite a while figuring it out: <!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>Date Menus</title> </head> <body> <?php // Script 10.1 - menus.php /* This script defines and calls a function. */ // This function makes three pull-down menus for the months, days, and years. function make_date_menus($n, $v = 10) { // Array to store the months: $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">'; for ($y = $n; $y <= ($n + $v); $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('1980', '20'); print '</form>'; ?> </body> </html> The above would then place 1980 as the year and 20 subsequent years in the drop-down box. It also has a default value of 10 years. If I wanted to pass the function variables instead of literal values i guess I'd use another script with a form asking for the year and current year in a form then pass the variables to the make_date_menus function?
  7. If terms is not set then it will not equal 'yes', as it uses the AND operator this if conditional would never be true. The second if conditional uses the OR operator, i.e. is variable 'terms' set OR is variable not equal to 'yes'. I'm sure more experienced programmers will explain this better!
  8. H Larry, I too am struggling to understand the objective of this task. This is what i do understand from the orginal example: The customize.php shows the drop-down boxes where the user picks the font size and color if cookies do not exist. On submit(page load) it then checks to see if any cookies exist by checking the $msg variable. If true it displays the message with link to view_settings.php. View_setting.php then shows the formatted text with links back to customize.php and reset.php. So, dosn't the view_settings.php page already reflex the users preferences, hence shows the formatted text? Please explain as I'm a little confused
  9. My mercury mal server wasn't enabled when I sent the mail locally? Is this normal? I thought sending mail wasn't possible without a mail server, or is the mail server my ISP?
  10. That worked a treat, thank you so much Didn't think it would be so simple to set up, I could have wasted so many hours trying to get that to work.
  11. The email address of the ISP I have (my ISP is plusnet so rob@plusnet.com) or any email address I own (@gmail.com etc.)?
  12. @Larry - Just out of interest how do you normally test your webpages in IE for browser compatibiity?
  13. @Jonathon - Thanks a bunch, that hopefully will be useful and I'll be able to send mail locally. Is this the method you used to send email locally in a Wndows environment? After setting this up would I have to send mail to my ISP email address e.g. to my plusnet email address or would it work fr gmail/hotmail etc? Also did you have to specify the headers when coding the mail function as suggested later on in the tutorial?
  14. From my basic knowledge and brief reading isset() checks to see if a value has been set and empty() checks to see if a variable is empty, 0 or NULL? Please correct me if I'm wrong Larry!
  15. I'm still a little confused understanding the differences between the empty() or isset() functions. In the above you suggest using the isset() clause on the 'email' variable, would it not work with empty()?. Think I remember reading you'd use isset on checkboxes, radio buttons alike. Please explain?
  16. I'm curently going through Chapter 8 and learning about the mail() function. As i expected the script produces a warning message when I try to send mail locally to gmail, hotmail or any mail server. As Larry mentions in the book this isn't a problem as you'll never host the website locally, I'm just interested to hear if anyone managed to get this to work? Spent a bit of time fiddlng last night, not going to waste too much time on it though, far too much learning to be done!!
  17. I use this feature all the time when using IE. I mainly use it to check out source code and how the CSS elements effect the page. Its also usful to view Javascript scripts too.
  18. I love the balancing braces function in notepad++ too. Not really sure what the differences are between a text editor and an IDE yet. I'm guessing an IDE has more complex programming functionalities built in? For a beginner I'd imagine a regular text editor like notepad++ or TextWrangler is sufficient?
  19. I have both a mac and pc (laptop) in my household. I'm currently using my laptop more out of convenience to learn php. I've always used notepad++ for my web projects in HTML and CSS. For the Mac I use TextWrangler. What text editor do you use? And are the ones I currently use sufficient for php development? Keep in mind I’m just a beginner.
  20. Thanks Larry, just one question, why do you have to pass the argument ($array) within printArray? And would I call it in the script by typing - printArray($array); ?
  21. Having worked through the sort function example in Chapter 7 I noticed there's a call to print out the array 3 times using the same code. Could this be a case for a custom made function, or is there little point? If so how would the function look? I'm just curious as I've not yet touched functions in the book yet. Just thinking ahead This is my code: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>Array - Sort</title> </head> <body> <div> <?php //Create array $team = array( 'Ba' => 9, 'Cabaye' => 4, 'Colocinni' => 2, 'Krul' => 1, 'Ben arfa' => 10, 'Jonas' => 18 ); //Print array before sort foreach ($team as $name => $number) { print "<p>$name, $number</p>"; } //Sort array by name ksort($team); ?> </div> <div> <?php //Print sorted array foreach ($team as $name => $number) { print "<p>$name, $number</p>"; } //Sort array by number asort($team); //Print sorted array foreach ($team as $name => $number) { print "<p>$name, $number</p>"; } ?> </div> </body> </html>
  • Create New...