Results 1 to 5 of 5

Thread: Sessions

  1. #1
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default Sessions

    Ok, here's the deal:
    I've created three pages to help me learn sessions.
    Page one: index.php
    Page two: page2.php
    Page three: page3.php
    I'll be blunt: I am totally and utterly confused.
    The example, http://www.flamehtmlstudios.com/session/.

    Ok, here's the code.
    Index.php is
    just a simple form that submits the person's name to page2.php, which is:

    PHP Code:
    <?php

    // PAGE2.PHP

    ini_set("bug_compat_42" "off");
    ini_set("session.bug_compat_warn" "off");

    $name $_REQUEST['name'] ;
    if(!
    $name) { 
        
    $name "You4gotAName";}
        
    setcookie('name' $name);

    session_start();
    $_SESSION['name'] = $name ;

    ?>

    <html>
    <head>
    <title>Session Practice, Page 2</title>

    </head>
    <body>

    <a href="page3.php"><?php echo $name ?>, Go To Page 3</a>.

    </body>
    </html>
    Now, the issue here is that $name is not showing as the person's name, but rather, literally, "$name". Why is this?!

    Ok, so, the $name is issue is the same for page3.php, which is below:

    PHP Code:
    <?php

    // PAGE3.PHP

    ini_set("bug_compat_42" "off");
    ini_set("session.bug_compat_warn" "off");

    $name $_COOKIE['name'];
        if(!
    $_COOKIE['name']) {
        echo 
    "No Name" ;}

    session_start();
    $_SESSION['name'] = $name ;

    echo 
    "

    <html>
    <head>
    <title>Session Practice, Page 2</title>

    </head>
    <body>

    You've reached the end of the line, 
    $name"."!

    </body>
    </html> "
    ;

    unset(
    $_SESSION['name']) ;

    ?>
    Ok, again, the $name issue is present here.
    Also, I know I'm supposed to end the session.
    How do I do this?

    Now, I'm so confused.
    First of all, I know I probably don't need the cookie stuff in there at all, since PHP automatically drops a cookie, and if cookies aren't enabled, puts a file with the session id on the server, correct?

    Ok, some help here?!

  2. #2
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by alexjewell
    ini_set("bug_compat_42" , "off");
    You missed 'session.' from that first string.

    The recommended configuration disables that bug. Are you sure you need to do it at run-time?

    ini_set("session.bug_compat_warn" , "off");
    Leaving the warning on won't hurt.

    $name = $_REQUEST['name'] ;
    If data should only be recieved by POST request, use the $_POST superglobal.

    if(!$name) {
    $name = "You4gotAName";}
    Ideally, this should be written more like:

    PHP Code:
    $name = !empty($_POST['name']) ? $_POST['name'] : 'Foo'
    If the element doesn't exist, attempting to access it will generate a notice in the logs (given the right logging level). The isset function can be used instead of empty if it's more appropriate.

    Both of these points apply to the other page, too.

    Now, the issue here is that $name is not showing as the person's name, but rather, literally, "$name". Why is this?!
    I don't see that, nor any obvious reason why it should.

    $name = $_COOKIE['name'];
    if(!$_COOKIE['name']) {
    echo "No Name" ;}
    You should be reading from the session, not a cookie.

    echo "

    <html>
    Why not jump out of PHP parsing mode, like you did in the previous example?

    unset($_SESSION['name']) ;
    You do this without checking that the element exists. If it doesn't, you code will trigger a fatal error.

    Also, I know I'm supposed to end the session.
    How do I do this?
    The abbreviation RTFM comes to mind here. The documentation for the session_destroy function describes how to invalidate a session entirely.

    First of all, I know I probably don't need the cookie stuff in there at all,
    Correct.

    since PHP automatically drops a cookie,
    If it's configured to do so.

    and if cookies aren't enabled, puts a file with the session id on the server, correct?
    No. Session data - what you add to the $_SESSION superglobal - is (usually) stored in a file on the server. The session identifier is transmitted via cookie (if enabled) and (if permitted), PHP will alter form actions and links if cookies are disabled to accomplish the same. Again, this (session id propagation) is described in the documentation.

    Mike

  3. #3
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    Wow, thanks so much for all of that.
    I have since fixed these issues (and others) and you can view the full working example by the link above.
    Thanks again.

  4. #4
    Join Date
    Jun 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by alexjewell
    Wow, thanks so much for all of that.
    I have since fixed these issues (and others) and you can view the full working example by the link above.
    Thanks again.
    hello. am a newbie to php, esp sessions. would you be so kind as to detail to me your working script? i sent you a pm. thanks in advance.

  5. #5
    Join Date
    Jun 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by webmasterneo
    hello. am a newbie to php, esp sessions. would you be so kind as to detail to me your working script? i sent you a pm. thanks in advance.
    I already got the script to work (except for the last page).. so if you do send me your script, i'll use it for comparison.. thank you again.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •