Jump to content

Recommended Posts

Posted

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

Posted

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>";

Posted
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?

B) 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

Posted

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

Posted

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().'">

Posted

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...