SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Thread: Sessions

  1. #1
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I am using this code from php.net

    PHP Code:
    <?php
    session_register 
    ("count");
    $count++;
    ?>

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

    <php?
    # the <?=SID?> is necessary to preserve the session id
    # in the case that the user has disabled cookies
    ?>

    To continue, <A HREF="nextpage.php?<?=SID?>">click here</A>
    It works, but the session id is not added to the url. I just get nextpage.php?

    Can anybody help
    Sean

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There are a couple problems with your code, first you aren't printing any variable, SID should be $SID if that is what session.name is set to in the php.ini file. Normally by default its $PHPSESSID, and unless you or your host has changed it should still be $PHPSESSID Also in order for your script to get the value it must look like

    Code:
    To continue, <A HREF="nextpage.php?PHPSESSID=<?=$PHPSESSID?>">click here</A>
    Or if you sure uses SID then

    Code:
    To continue, <A HREF="nextpage.php?SID=<?=$SID?>">click here</A>
    Also the reason it works is because it sets a session cookie to your computer to hold the session id, so the only reason you would need to pass the session id in the url is if the user has cookies turned off.
    Last edited by freddydoesphp; May 21, 2001 at 13:00.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Is this the first time you have ever used sessions? If it is, you have to change something in your php.ini file. Look for these lines:

    [Session]
    ; Handler used to store/retrieve data.
    session.save_handler = files

    ; Argument passed to save_handler. In the case of files, this is the path
    ; where data files are stored.
    session.save_path = "d:\phpSessions2"

    You have to create a file anywhere on your computer for sessions to save variable/value information with the session id. You have to change the value of session.save_path from the default "/tmp" to a folder you create on your computer. As you can see, the path to my folder for session information is:

    d:\phpSessions2

    Otherwise, everything looks fine with your code (except for: <php?)
    I am afraid freddydoesphp was incorrect when he said you did not have the proper syntax with this statement:

    <A HREF="nextpage.php?<?=SID?>">

    That is the format that the php manual recommends even though it looks strange. See here freddy:

    http://www.php.net/manual/en/ref.session.php

    The constant SID has this string in it:

    "PHPSESSID=$PHPSESSID"

    <? and ?> are deprecated php tags, and

    =SID

    is the same as:

    echo SID

    AND the last line of php before a closing php tag does not need a semicolon....lol. It is a horrible construct!
    Last edited by 7stud; May 21, 2001 at 19:08.

  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm.. weird, you are correct 7stud, although when I used this code:

    PHP Code:
    session_start();
    print 
    SID
    I get

    SID=44kjhnb42kjh42jhsfv8ew3

    but if I refresh I get nothing, seems a little inconsistent. Anyways I usually just let the cookies handle it, and I have --enable-trans-sid for the weenies who have cookies turned off.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  5. #5
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi freddy,

    I am not sure the exact mechanism of how SID works. As your test shows php can tell what the context is somehow. I don't know if you saw the end of my post because you responded so quickly.

  6. #6
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    The code is straight from the PHP manual Freddy. I have just started using sessions and everything works fine apart from SID. I was just playing about with passing the session ID in the url and was just wondering if anyone knew why SID did not work.

    ... the last line of php before a closing php tag does not need a semicolon....lol. It is a horrible construct!
    Better tell them to change the manual then!

    Otherwise, everything looks fine with your code (except for: <php?)
    Yeah, I changed this but put the original code here, DOH!

    Sean

  7. #7
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    seanf don't forget the user postings in the manual are from guys and gals just like us, so don't expect them to be perfect.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  8. #8
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    If you go to the session page http://www.php.net/manual/en/ref.session.php it is example number 3.

    Sean

  9. #9
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I tried your script and no session id appeared in the url for me either. So,
    I disabled cookies,and then when I tried the script I got:

    Code:
    http://localhost/nextpage.php?PHPSESSID=
    db2e9e3c27a2fc96ed0acdf3d43838e3&PHPSESSID=
    db2e9e3c27a2fc96ed0acdf3d43838e3
    lol...the session id appeared twice. Then when I turned cookies back on,
    the session id was still there twice. I don't know
    if php tries to detect whether it needs to include
    SID in the url or not.
    Last edited by 7stud; May 22, 2001 at 12:06.


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
  •