Jump to content
Larry Ullman's Book Forums

timpearson26

Members
  • Posts

    21
  • Joined

  • Last visited

  • Days Won

    1

Posts posted by timpearson26

  1. I have recently finished your book PHP for the web and am now starting this new book. When I originally installed MAMP on my computer quite a few months ago, I installed mysql version 5.1.xx. I noticed now in this book that your are using mysql version 5.5.8.

     

    Is there any reason to update mysql to the latest version? I looked just briefly on the internet for a how to and it didn't seem to be very straightforward.

     

    Any suggestions?

     

    Thanks.

     

    Tim

  2. Sessions or the sessions array would be used rather than cookies which can easily be exploited. If you get Larry's next book php and mysql for dynamic websites you will see this in action. Or for more advanced stuff you can get his advanced php book that handles session info stored in the database to be extra secure.

     

    Thanks for the reply. Yeah, I already have the next book, looking forward to starting it soon.

     

    Thanks.

  3. In the Chapter 13 web app, cookies are used to verify if a person has administrator access.

     

    It seems that using a cookie is similar to a password in the way that in the book, Larry says to set a cookie with sort of a random name and value. For instance, don't set a cookie with the name of 'login' and the value of 'true' (instead a cookie named Samuel is set with a value of Clemens). But, because cookies are easily viewed once they are set, for example using firebug on firefox, it seems like this is not the best method for veirifying who has access to a site and who doesn't.

     

    For example. Lets say someone signs up for a username and password on my site, I grant that person permission to my site and set a cookie named Samuel with a value of Clemens. But lets say for some reason in the future I choose to deny that user access to my site. If while he had access to my site, he happened to check the name and value of the cookie, that person after he looses access to my site could easily create a cookie himself named Samuel with a value of Clemens. Then what?

     

    Is this the method that websites actually use to verify login credentials? (obviously I know this is a beginner book and there is probably much more to it than this, but I was wondering if this was an easy way to mimic a login example, or if some form of this method is used in professional sites.)

  4. the last pursue question says: use the combination of writing to and reading from text files, plus either sessions or cookies, to create a real registration and login system.

     

    I am having a problem wrapping my head around the concept of using the session in the registration process and how you would make this unique to the person logging in.

     

    What i understand so far is this:

     

    - when a person registers, their username and password gets written to a file

    - when they loggin the script checks the just entered username and password to see if it matches with a username and pass on file

    - if it does match I should send a cookie saying something like $_COOKIE['logged_in'] = TRUE.

    - then in the follwing pages of the website, I should check for this 'logged_in' cookie to see if it is there.

     

     

    What I don't get is how to make the logged_in cookie personal to the specific user. For example, when I login to my bank website, I want to see my money, not another users money. Or is this pursue question just asking for the baby step of creating this generic cookie only, and later on we will learn how to make it specific.

     

    Thanks for the help.

     

    Tim

  5.  

    What's the connection of $okay now being FALSE to the nonprinting of the message? Thanks so much for all your help!!!

     

     

    Do you understand how the 'if' conditional works? When writing the 'if' conditional, if whatever is in the parentheses is TRUE, the following lines of code will be executed. But if whatever is in the parentheses is FALSE, the following lines of code will not be executed.

     

    For example:

     

    if (1 == 1) {

    print '<p>You have been successfully registered (but not really).</p>';

    }

     

    (this above example will print)

    but this example:

     

    if (1 == 2) {

    print '<p>You have been successfully registered (but not really).</p>';

    }

     

    this will not print because, obviously, 1 does not equal 2.

     

     

    So in the code on page 123. You see on line 20 the flag variable is created and is given the value of TURE. At this point if everything goes good, the success message will print. BUT, before the success message prints, you first have to get past the other 2 'if' statements. Those 'if' statements validate the email and password. If either one of those 'if' statements has a value of TRUE (in other words, if either the $_POST['email'] or the $_POST['password'] is empty), then the code below those 'if' statements execute. And both of those validation 'if' statements do 2 things. First, they print an error message, and 2nd, they change the value of $okay to the value of FALSE. If that happens, you will not see the success message because now:

     

    if ($okay) { ...}

     

    has the value of

     

    if (FALSE) { //therefor nothing happens }

     

     

     

    Hopefully that makes it a little more clear.

     

    Tim

    • Upvote 1
  6. Wow, just got through reading all of the back and forth here. Now my brain hurts :) I got the same answer as phpRob for the pursue #3. However, it looks to me that April, you did not correctly answer the Pursue #2 question. (also phpRob, your above script doesn't answer Pursue #2 question, but perhaps you were just intending to answer #3 and you left the code out for the #2 answer for simplicity, not sure). Anyway the code that I came up with that answers Pursue #2 and #3 is this:

     

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3org/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>Sticky Text Inputs</title>
    </head>
    <body>
    <?php  //script 10.2  - sticky1.php	- sticky2.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, $text_or_pass = 'text', $size = 20) {
    
    //begin a paragraph and a label:
    print '<p><label>' . $label . ': ';
    
    //Begin the input
    print '<input type="' . $text_or_pass . '" name="' . $name . '" size="' . $size . '" ';
    
    //add the value
    if (isset($_POST[$name])) {
     print ' value="' . htmlspecialchars($_POST[$name]) . '"';
    } elseif (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', '', 30);
    make_text_input('email', 'Email Address', '', 50);
    make_text_input('password', 'Password', 'password');
    print '<input type="submit" name="submit" value="Register!" /></form>';
    
    
    ?>
    </body>
    </html>
    

     

    April, you need to have this code:

     

    elseif (isset($_GET[$name])) {

    print ' value="' . htmlspecialchars($_GET[$name]) . '"';

    }

     

     

    to check for $_GET values. Otherwise if the form method = GET your form won't be sticky.

     

    Did anyone else get a similar answer?

     

    Tim

    • Upvote 1
  7. You might also want to check out the variable scope section a little further on in chapter 10.

     

    Thanks. Yeah I stopped for the night just before the 'Variable Scope' section :). After just reading it, it makes a lot more sense. What exactly answered my question was this:

     

    page 279: Function variables - the arguments of a function as well as any ariables defined within the function - exist only within that function and aren't accessible outside of it.

     

    also page 280: Because of variable scope, a local variable within a function is a different entity than a variable outside of the function, even if the two variables use the exact same name.

     

    Thanks.

  8. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3org/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>Cost Calculator</title>
    </head>
    <body>
    <?php  // Script 10.4 - calculator.php
    /* this script displays and handles an HTML form.
    It uses a function to calculate a total from a quantity and price. */
    //this function returns the calculations
    function calculate_total ($quantity, $price) {
    $total = $quantity * $price; //calculation
    $total = number_format ($total, 2); //formatting
    
    return $total;  //return the value.
    
    } //end of calculate_total() function
    //check for form submisstion
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    
    //form validation
    if (is_numeric($_POST['quantity']) && is_numeric($_POST['price'])) {
    
     //call the function and print the results
     $total = calculate_total($_POST['quantity'], $_POST['price']);
     print "<p>Your total comes to $<span style=\"font-weight: bold;\">$total.</span></p>";
    
    } else {
    
     print '<p style="color: red;">Please make sure to enter only numbers into the calculator.</p>';
    
    } // end of form validation
    
    }  // end of if checking form has been submitted.
    
    ?>
    <form action="calculator.php" method="POST">
    <p>Quantity: <input type="text" name="quantity" size="3" /></p>
    <p>Price: <input type="text" name="price" size="3" /></p>
    <p><input type="submit" name="submit" value="Calculate!" /></p>
    </body>
    </html>
    

     

    Are variables located inside of functions separate from variables located outside of functions?

     

    For example in the above code, the calculate_total() function returns the variable $total. But later on in the script, I assign $total to this:

     $total = calculate_total($_POST['quantity'], $_POST['price']);   

     

    By doing that am I overwriting the $total variable? My guess is that the answer is no, because I tried changing the $total variable to something different, for example to $sum, like this:

    //call the function and print the results
     $sum = calculate_total($_POST['quantity'], $_POST['price']);
     print "<p>Your total comes to $<span style=\"font-weight: bold;\">$sum.</span></p>"; 

     

    and the function still worked properly. I was just looking for a little clarification on this.

     

    Thanks.

     

    Tim

  9. to ashez2ashes,

     

    I started this book (PHP for the web: visual quick start guide) about 2 months ago and I am now in the middle of chapter 8. So far I have found this book explains everything very well. BTW, I am a total beginner. When I started this book, I didn't know html or css, but as I have been going through the book, I think the html and css used by larry in the book is pretty self explanatory. If it was not, I did minor research on the web (but again this was because I did not meet the pre-reqs of knowing html and css first).

     

    But I do see where you did get confused. Larry reccomends to go look at the php manual early on in the book. When I did that I found it to be very confusing. One tip though. http://tw2.php.net/m....prototypes.php Look here. It is on 'how to read a function definition' . I found this very helpful in understanding the php manual. Frankly, I think page should not be so deeply buried.

  10. phpRob, the only thing I see in your fourth task that you can improve is that you first declared the variables from $_POST, but then when you validated the day and the month, you still called the values using $_POST instead of using the new variable name.

     

    oh also at the end when you create the $dob variable, you can add a / in between your two single quotes to make the date look exactly like what the book is asking for. Like this:

     

    $dob = $month . '/' . $day . '/' . $year;

    But maybe that is just being nit-picky

  11. I came up with this. Basically it is the same as what you have, except that I made another switch so I could print out the "your favorite color is this" statement where I wanted it. Also instead of escaping the trouble characters, I did the print statement with single quotes and concatenation. Maybe that is what the book was looking for.

     

     

    switch ($color) {

    case 'red':

    print '<p class="red">Your favorite color is ' . $color . '</p>';

    break;

    case 'yellow':

    print '<p class="yellow">Your favorite color is ' . $color . '</p>';

    break;

    case 'green':

    print '<p class="green">Your favorite color is ' . $color . '</p>';

    break;

    case 'blue':

    print '<p class="blue">Your favorite color is ' . $color . '</p>';

    break;

    default:

    break;

    }

×
×
  • Create New...