Page 1 of 2
PHP session problems!
Posted: Wed Feb 21, 2007 8:00 am
by timbrown
Everah | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Hi,
I am having trouble with sessions.
Sessions work fine on one page, but when I navigate to another page they don't appear to be set. I'm sure there's something simple i'm doing wrong but I've been trawling around for some time now and got nowhere.
The session files do appear correctly in C:\php\sessiondata each time the page is refreshed.
Any help would be great!
Here's the code simplified -
page 1...
works fine
Code: Select all
<?php
session_start();
$_SESSION["name"] = "tim";
echo $_SESSION["name"];
?>
<a href="page2">go there</a>
page 2...
message says
_Session variable is not set
This has been doing my head in for 2 days!
Thanks. Tim.
Everah | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Posted: Wed Feb 21, 2007 8:07 am
by volka
Hi.
try
Code: Select all
<?php
error_reporting(E_ALL); ini_set('display_errors', true); // for debugging
session_start();
echo $_SESSION["name"];
?>
session_start() does not create a new session but starts php's session mechanism. You have to call it once each time php is called (~ for each http request)
p.s.: please use
Code: Select all
[url=http://forums.devnetwork.net/faq.php?mode=bbcode]bbcode tags[/url] when posting php code.
Posted: Wed Feb 21, 2007 8:10 am
by Oren
Simple... indeed.
Chane page 2 to this:
Code: Select all
<?php
session_start();
echo $_SESSION["name"];
?>
P.S Use the BBCode to highlight your code. Use the 'PHP' button to highlight your php code so it will look like the code in this post

Posted: Wed Feb 21, 2007 8:35 am
by timbrown
Oren wrote:Simple... indeed.
Chane page 2 to this:
Code: Select all
<?php
session_start();
echo $_SESSION["name"];
?>
P.S Use the BBCode to highlight your code. Use the 'PHP' button to highlight your php code so it will look like the code in this post

Hi, thanks for the reply.
I now get the following message in page 2.
Undefined index: name
It's as if the session is not carried over to the next page.
any ideas?
Posted: Wed Feb 21, 2007 8:44 am
by feyd
Was the session able to start on the second page?
Posted: Wed Feb 21, 2007 8:47 am
by volka
please try
Code: Select all
<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
session_start();
if ( !isset($_SESSION['xyz']) || !is_array($_SESSION['xyz']) ) {
$_SESSION['xyz'] = array();
}
$_SESSION['xyz'][] = date('H:i:s');
?>
<html>
<head><title>session test</title></head>
<body>
<a href="<?php echo $_SERVER['PHP_SELF']; ?>">re-load page</a><br />
session_name: <?php echo session_name(); ?><br />
session_id: <?php echo session_id(); ?><br />
<pre>cookies: <?php var_export($_COOKIE); ?></pre>
<pre>session: <?php var_export($_SESSION); ?></pre>
</body>
</html>
when you press the link and reload the page session_name and session_id shouldn't change but a new line should be appended to session: each time. Does this work?
Posted: Wed Feb 21, 2007 9:11 am
by timbrown
feyd wrote:Was the session able to start on the second page?
when I print_r on page 1 I get Array (name=>tim), but when I print_r on page 2 I get array()
Does this mean page 2 resets the session?
Posted: Wed Feb 21, 2007 9:13 am
by timbrown
volka wrote:please try
Code: Select all
<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
session_start();
if ( !isset($_SESSION['xyz']) || !is_array($_SESSION['xyz']) ) {
$_SESSION['xyz'] = array();
}
$_SESSION['xyz'][] = date('H:i:s');
?>
<html>
<head><title>session test</title></head>
<body>
<a href="<?php echo $_SERVER['PHP_SELF']; ?>">re-load page</a><br />
session_name: <?php echo session_name(); ?><br />
session_id: <?php echo session_id(); ?><br />
<pre>cookies: <?php var_export($_COOKIE); ?></pre>
<pre>session: <?php var_export($_SESSION); ?></pre>
</body>
</html>
when you press the link and reload the page session_name and session_id shouldn't change but a new line should be appended to session: each time. Does this work?
thanks for putting that together.
In fact the session ID does change each time I refresh the page. nice eh?
Posted: Wed Feb 21, 2007 9:13 am
by feyd
timbrown wrote:when I print_r on page 1 I get Array (name=>tim), but when I print_r on page 2 I get array()
Does this mean page 2 resets the session?
Possibly. How do the URLs of page 1 and page 2 relate?
Posted: Wed Feb 21, 2007 9:22 am
by timbrown
feyd wrote:timbrown wrote:when I print_r on page 1 I get Array (name=>tim), but when I print_r on page 2 I get array()
Does this mean page 2 resets the session?
Possibly. How do the URLs of page 1 and page 2 relate?
They are just 2 php files in the same folder with an <a href... from one to the other.
Posted: Wed Feb 21, 2007 9:27 am
by volka
timbrown wrote:In fact the session ID does change each time I refresh the page. nice eh?
No. New session id means new session, new set of data.
Does the output behind
cookies: change as well? If yes your browser is probably rejecting all cookies and php is unable to store the session id client-side.
Posted: Wed Feb 21, 2007 9:30 am
by feyd
timbrown wrote:In fact the session ID does change each time I refresh the page. nice eh?
That would suggest your browser is not accepting the session cookie and your server is set to not use the URL as a fall back.
session.use_trans_id among others will affect the behavior. You can use phpinfo() to see what options you have currently set.
Posted: Wed Feb 21, 2007 9:47 am
by timbrown
volka wrote:timbrown wrote:In fact the session ID does change each time I refresh the page. nice eh?
No. New session id means new session, new set of data.
Does the output behind
cookies: change as well? If yes your browser is probably rejecting all cookies and php is unable to store the session id client-side.
Yes the time does change in the cookies area. I will check browser settings again and come back.
thanks.
Posted: Wed Feb 21, 2007 9:56 am
by timbrown
I have checked the browser, and it is set to accept first party & third party cookies and always allow session cookies. anything else?
Posted: Wed Feb 21, 2007 10:03 am
by timbrown
timbrown wrote:I have checked the browser, and it is set to accept first party & third party cookies and always allow session cookies. anything else?
Hey I got it too work by changing my cookie_path to / which is the default anyway!
thanks for all your help!
Tim.