PHP session problems!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

timbrown
Forum Newbie
Posts: 14
Joined: Wed Feb 21, 2007 7:48 am

PHP session problems!

Post by timbrown »

Everah | Please use

Code: Select all

,

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

Code: Select all

<?php
echo $_SESSION["name"];
?>
This has been doing my head in for 2 days!

Thanks. Tim.


Everah | Please use

Code: Select all

,

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]
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post 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 :wink:
timbrown
Forum Newbie
Posts: 14
Joined: Wed Feb 21, 2007 7:48 am

Post 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 :wink:
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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Was the session able to start on the second page?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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?
timbrown
Forum Newbie
Posts: 14
Joined: Wed Feb 21, 2007 7:48 am

Post 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?
timbrown
Forum Newbie
Posts: 14
Joined: Wed Feb 21, 2007 7:48 am

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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?
timbrown
Forum Newbie
Posts: 14
Joined: Wed Feb 21, 2007 7:48 am

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
timbrown
Forum Newbie
Posts: 14
Joined: Wed Feb 21, 2007 7:48 am

Post 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.
timbrown
Forum Newbie
Posts: 14
Joined: Wed Feb 21, 2007 7:48 am

Post 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?
timbrown
Forum Newbie
Posts: 14
Joined: Wed Feb 21, 2007 7:48 am

Post 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.
Post Reply