Thread: sessions

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Location
    USA
    Posts
    226
    Rep Power
    15
    I am new to sessions and don't really understand why something is happening. I have created a form with name and address inputs. I have created the variables as:

    session_register ("name");
    session_register ("address");

    These variables flow to the next page just fine. However, if I go back to the form and edit the information, the new information does not replace the old information. It defaults back to the original entry. Am I doing something wrong?

    A sample of the code is as follows:

    Page1 -

    <?
    session_start()
    ?>

    <?
    session_register ("count");
    session_register ("name");
    session_register ("address");
    $count++;
    ?>

    Hello visitor, you have seen this page <? print $count; ?> times.<p>

    <form action="sess2.phtml?<?=SID?>" method="post">
    Name: <input type="text" name="name" value="<? print($name) ?>"><br>
    Address: <input type="text" name="address" value="<? print($address) ?>">
    <input type="submit"><input type="reset">
    </form>

    To continue, <A HREF="sess2.phtml?<?=SID?>">click here</A>

    Page2 -

    <?
    session_start()
    ?>

    <html>
    <head>
    <title>Untitled</title>
    </head>

    <body>
    <? print($count) ?><br>
    <? print($name) ?><br>
    <? print($address) ?><br><br>

    To go back, <A HREF="sess1.phtml?<?=SID?>">click here</A>


    </body>
    </html>

    Thanks for any help that you may have.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Posts
    51
    Rep Power
    15
    Hey bramsey,

    Did you compile PHP with --enable-track-vars? If not, the SID will not be created.

    Assuming that you have... Here is the working code:

    sess1.phtml
    <?
    session_start();
    if ((! $count) && (! $name) && (! $address)) :

    session_register ("count");
    session_register ("name");
    session_register ("address");

    endif;

    $count++;
    ?>
    Hello visitor, you have seen this page <?=$count;?> times.<p>
    <form action="sess2.phtml?<?=SID?>" method="post">
    Name: <input type="text" name="name" value="<?=$name;?>"><br>
    Address: <input type="text" name="address" value="<?=$address;?>">
    <input type="submit"><input type="reset">
    </form>
    To continue, <A HREF="sess2.php?<?=SID?>">click here</A>

    sess2.phtml
    <?
    session_start()
    ?>
    <html>
    <head>
    <title>Untitled</title>
    </head>
    <body>
    <?=$count;?><br>
    <?=$name;?><br>
    <?=$address;?><br><br>
    To go back, <A HREF="sess1.php?<?=SID?>">click here</A>

    </body>
    </html>


    hope this helps,
    wj gilmore
    wjgilmore@hotmail.com


  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Location
    USA
    Posts
    226
    Rep Power
    15
    Yes, track_vars is on in my php.ini file. Is there somewhere else where I need to enable it? I tried your code and it does the same thing. The variables go through to the second page fine. However, if I go back to the form on the first page and change the name and address, the new entries do not go to the second page. The old entries remain. I have looked in the tmp directory and the original variables are in the cookie. There has got to be something that I am missing. How do I make changes on the form override my original entries?

  6. #4
  7. No Profile Picture
    Apprentice Deity
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 1999
    Location
    Niagara Falls (On the wrong side of the gorge)
    Posts
    3,237
    Rep Power
    19
    Your problem is that you are using the same variable names in the session and the form.

    So you have two sets of variables with the same names being passed to the second script, one set from the session and one set from the form using POST. Since session variables have precedence over POST variables the POST variables of the same name are overwritten. You need to use different variable names in your form, say name2 and address2 then in the target script:

    session_start();
    if ($name2) $name=$name2;
    if ($address2) $address=$address2;

  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Location
    USA
    Posts
    226
    Rep Power
    15
    Thanks rod k, you are awesome. It worked!

Similar Threads

  1. Why do sessions dissappear when entering a ssl webpage
    By cuboctahedron in forum PHP Development
    Replies: 3
    Last Post: July 2nd, 2003, 09:43 PM
  2. multipage form: sessions or hidden fields?
    By Ssy in forum PHP Development
    Replies: 7
    Last Post: June 24th, 2003, 11:20 AM
  3. Creating user sessions w/out using sessions
    By marron79 in forum PHP Development
    Replies: 5
    Last Post: September 13th, 2002, 06:16 PM
  4. Membership system using sessions and cookies
    By m0nk3y in forum PHP Development
    Replies: 6
    Last Post: March 26th, 2002, 06:24 PM
  5. Replies: 4
    Last Post: September 16th, 2001, 11:48 AM

IMN logo majestic logo threadwatch logo seochat tools logo