/    Sign up×
Community /Pin to ProfileBookmark

Lifespan of $_SESSION Variable

I’m trying to keep a php variable alive between a number of pages. I was under the impression that assigning the value to $_SESSION[‘sample_var’] was the way to go. I’ve found that between 3 pages, all of which begin with <?php session_start();?>, the value of the $_SESSION[‘sample_var’] is retrievable on page 2, but is lost on page 3. Does this make sense, meaning I am not able to echo anything to the screen. Does this make sense?

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJan 05.2005 — A few possibilities:
[list]
  • [*]You've got a typo somewhere and aren't really starting the session on page 3. (Never happens to me. :rolleyes: )

  • [*]You somehow destroy your session and/or unset the variable before you go to page 3.

  • [*]You have a very short expiration time on your session cookies.

  • [*]You've found bug in PHP.
  • [/list]

    Anyway, based on my understanding and experience, it [i]should[/i] work as you described it.
    Copy linkTweet thisAlerts:
    @dmichaelauthorJan 06.2005 — NogDog, about that 3rd possibility you list...when I got to page 3, I echoed phpinfo(). It lists session.cookie_lifetime = 0 (local) and 0 (master). Is this what you mean? I can't find any typos, as I did a copy & paste. If it's the session.cookie_lifetime, how dow I change the values, and to what do I change them?
    Copy linkTweet thisAlerts:
    @PWDJan 06.2005 — Do you have [code=php]session_id();[/code] as the very first line of your subsequent pages? (after <?php of course)

    Should look something like this..

    [code=php]

    <?php
    session_id();
    session_start();
    header("Cache-control: private"); //IE6+ fix

    if (!session_is_registered("Visitor") ) {
    session_register("Visitor");
    }

    if (!isset($_SESSION['Time'])) { //MY session variables
    $_SESSION['Time'] = date("l F j, Y - g:i a");
    }

    if (!isset($_SESSION['browser'])) { //MY session variables
    $_SESSION['browser'] = getenv("HTTP_USER_AGENT");

    }

    ?>
    [/code]


    You'll need to call that function in order to carry the unique session id through to your other pages. (don't forget the session_start() as well on EVERY PAGE)

    PWD
    Copy linkTweet thisAlerts:
    @scragarJan 06.2005 — another thing to check is the presence of a session ID in the url, although not esential you should always include it if you have ANY problems regarding sessions expireing early.

    [code=php]<a href="page2.php?<?
    echo(SID);
    ?>">go to page 2.</a><br>
    <!-- OR -->
    <form action="page3.php?from=<?
    echo($PHP_SELF."&".SID);
    ?>" method=GET><input type=submit value="go to page 3"></form>
    <!-- remember that for the redirect header you must use SID even if PHP says the cookies are enabled because these are not automaticly transfered. -->[/code]
    Copy linkTweet thisAlerts:
    @dmichaelauthorJan 09.2005 — HELP! I still haven't figured out what's happening as my $_SESSION variables are dying prematurely. In the process, I've tried to echo the SID, and got NOTHING! To review:

    Page 1 - Hidden field (package_choice) is assigned a value via Javascript before its FORM is submitted.

    Page 2 - I assign the value to a $_SESSION var using the following code:

    $SESSION['package_choice'] = trim($_
    POST['package_choice']);

    echo $SESSION['package_choice'];

    The echo shows the value on page 2.

    Page 3 - The following line displays NOTHING on page 3:

    echo $SESSION['package_choice'];

    On pages 2 & 3, I have the following at the top of both pages:

    <?php

    session_start();

    echo SID;

    ?>

    The echo displays NOTHING!

    Using the advice of SCRAGAR, I tried to ensure that the SESSION ID was made part of the URL, and based my coding on his ACTION example:

    ACTION="payment-option-page.htm?from=<?php echo($PHP_SELF."$".SID);?>"

    That line of code got me the following error:

    Parse error: parse error, unexpected '=' in c:program filesapache groupapachehtdocsorder-full-page.htm on line 344

    COULD SOMEONE EXPLAIN WHAT'S GOING ON HERE? THANKS!
    Copy linkTweet thisAlerts:
    @phpnoviceJan 09.2005 — [i]Originally posted by dmichael [/i]

    [B]On pages 2 & 3, I have the following at the top of both pages:

    <?php

    session_start();

    echo SID;

    ?>

    The echo displays NOTHING![/B]
    [/QUOTE]

    I had the same problem. SID was not being set by my webhost's configuration of the PHP environment. Thus, I changed to the following to verify the presence of the session id:

    echo "<p>Session ID = " . session_id() . "</p>n";
    Copy linkTweet thisAlerts:
    @dmichaelauthorJan 09.2005 — Okay phpnovice, session_id() does return the expected value (echoing SID worked the first time, then never again on subsequent tries). So now, what about my original problem...losing the value of $SESSION['package_choice'] on page 3??? The session ID is not part of the URL because I haven't been able to echo SID, so should I try to make it part of the URL using session_id()? If so, could someone show me the proper syntax using an HTML ACTION statement? Using SCRAGAR's example, I got a syntax error (see dmichael 2 postings back).
    Copy linkTweet thisAlerts:
    @phpnoviceJan 10.2005 — [i]Originally posted by dmichael [/i]

    [B]The session ID is not part of the URL because I haven't been able to echo SID, so should I try to make it part of the URL using session_id()?[/B][/QUOTE]

    Well, I am using the following at the beginning of [u]all[/u] of my pages (because there is no [u]one[/u] page that is used as an entrance to my store) in order to [b]prevent[/b] adding the session id to the links. Exposing the session id can be a security risk in some situations. &lt;?php
    ini_set('url_rewriter.tags', ''); // Tells the URL rewriter not to change anything
    ini_set('session.use_trans_sid', '0'); // Disables transparent SID support
    ini_set('session.use_only_cookies', '1');
    session_start();
    ?&gt;

    Thus, I've made my pages dependent upon client cookies being accepted by the visitor's browser. I use the following code on my shopping cart page to make sure the visitor's browser is accepting cookies. So, if not, the visitor gets redirected to a page telling them that cookies are required.
    &lt;?php
    if (isset($_REQUEST[session_name()])
    &amp;&amp; !empty($_REQUEST[session_name()])) {
    session_start();
    } else {
    header('Location: http&amp;#58;//'.$_SERVER['HTTP_HOST']
    .dirname($_SERVER['PHP_SELF'])
    .'/cookies.php';
    exit;
    }
    ?&gt;

    I'm not completely done testing yet, though, so the above may not be a complete scenario.
    ×

    Success!

    Help @dmichael spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.1,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,
    )...