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?
@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?
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)
@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]
@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:
@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:
@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).
@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. <?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(); ?> 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. <?php if (isset($_REQUEST[session_name()]) && !empty($_REQUEST[session_name()])) { session_start(); } else { header('Location: http&#58;//'.$_SERVER['HTTP_HOST'] .dirname($_SERVER['PHP_SELF']) .'/cookies.php'; exit; } ?> I'm not completely done testing yet, though, so the above may not be a complete scenario.