sylvest Posted September 16, 2008 Posted September 16, 2008 I can't get PHP to remember the $_SESSION array from one page to the next. I'm using session varaibles to remember login settings, but when the user goes to a second page eitehr by linking to it, or via a header redirect, or just typing in the URL, the $_SESSION array is empty. Here are a couple of test pages I've written: Page test1.php starts the session and sets a session variable: <?php //prevents caching header("Expires: Sat, 01 Jan 2000 00:00:00 GMT"); header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT"); header("Cache-Control: post-check=0, pre-check=0",false); session_cache_limiter(); session_start(); $_SESSION['myvar'] = 'abracadabra'; $page_title="Test Page 1"; echo "<html>"; echo "<head>"; echo "<title>" . $page_title . "</title>"; echo "</head>"; echo "<body>"; echo "<h1><center>" . $page_title . "</center></h1>"; print_r($_SESSION); echo "<p><a href='test2.php'>Go to Test2</a></p>"; echo "</body>"; echo "</html>"; ?> Page test2.php sould be able to remember the session variables, but doesn't: <?php //prevents caching header("Expires: Sat, 01 Jan 2000 00:00:00 GMT"); header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT"); header("Cache-Control: post-check=0, pre-check=0",false); session_cache_limiter(); session_start(); $page_title="Test Page 2"; echo "<html>"; echo "<head>"; echo "<title>" . $page_title . "</title>"; echo "</head>"; echo "<body>"; echo "<h1><center>" . $page_title . "</center></h1>"; print_r($_SESSION); echo "</body>"; echo "</html>" ?> What am I doing wrong? Thanks - Rowan Quote
carbonize Posted September 16, 2008 Posted September 16, 2008 So it's not even printing out the $_SESSION['myvar'] = 'abracadabra'; variable since thats the only thing being saved in the session? Quote
sylvest Posted September 16, 2008 Author Posted September 16, 2008 So it's not even printing out the $_SESSION['myvar'] = 'abracadabra'; The page test1.php prints out 'abracadabra'; the page test2.php doesn't. Try it at: http://www.grantachorale.org.uk/members2/test1.php Quote
carbonize Posted September 16, 2008 Posted September 16, 2008 Works fine here. Possibly it's because you have cookies disabled and so the session id is not being carried over. Try this line echo "<p><a href='test2.php?".SID."'>Go to Test2</a></p>"; Quote
sylvest Posted September 16, 2008 Author Posted September 16, 2008 Works fine here. Possibly it's because you have cookies disabled and so the session id is not being carried over. Try this line... That works. But a) as far as I know, I have cookies enabled. I'm using IE7 in Vista with its privacy set to Medium. My cookies folder has lots of current cookies in it - why is it rejecting my PHP session cookie if it accepts all of these? I thought PHP was meant to automatically switch to including the SID in the URL if cookies were not available. Why is it not doing this? c) I need the solution to this to be on the server, not the client, otherwise many members of the public won't be able to access my web site. I need people who are using normal browsers with normal settings to be able to access the site. Any advice on how to achieve this? Many thanks - Rowan Quote
carbonize Posted September 16, 2008 Posted September 16, 2008 a - Could be a localhost issue b - Only if it is enabled in php.ini c - Just add the SID constant to your urls and you will be fine. Quote
sylvest Posted September 16, 2008 Author Posted September 16, 2008 Another point: if I have to include the SID in the URL in case cookies are not available, what is the generic way of doing this? Presumably adding "?" . SID onto the end of URLs won't work if: the URL already has some GET parameters (then I'd have two '?'s which would be a syntax error) the URL points to a site or directory (with or without a trailing /) and relies on the server fetching the default document possibly if the page is using POST (can a page return parameters via both GET and POST?) others I may not have thought of... How do I write some PHP to add SID to the URL which will work with all types of URL? Thanks - Rowan Quote
carbonize Posted September 16, 2008 Posted September 16, 2008 If memory serves SID outputs the session ID as variable=id (PHPSESSID=fd8d8bb54c8e368fc5d831cc02979c38) but once you have started a session you can use session_id(); session_start(); $getsid1 = '?'.SID; $getsid2 = '&'.SID; $postsid = '<input type="hidden" name="PHPSESSID" value="'.session_id().'"> Quote
sylvest Posted September 16, 2008 Author Posted September 16, 2008 phpinfo() tells me that session.use_trans_sid is set to 0. This is presumably why it's not automatically including SID in the URLs. Can I change this config parameter just for my site on TCH, or is it a server-wide setting? I can't find any kind of local php.ini file... Thanks - Rowan Quote
TCH-Bruce Posted September 16, 2008 Posted September 16, 2008 Adding this to your .htaccess file should turn on session ids >php_flag session.use_trans_sid on Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.