Jump to content
Larry Ullman's Book Forums

phpRob

Members
  • Posts

    58
  • Joined

  • Last visited

  • Days Won

    2

Posts 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.  

    Shouldn't you both be putting something in the quotes for the action attribute?

     

     

    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 recommend taking the $months array assignment out of the function, the array is being reassigned each time the function is called. Even though the function is only called once, if it was called multiple times per page, you would be reassigning over and over, even though months do not change.

     

     

    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. 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>

  5. 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?

    • Upvote 1
  6. 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 :)

  7. @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?

  8. 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!!

  9. 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 B)

     

    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...