Sessions are lost on page redirect

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

fakhrizaki
Forum Newbie
Posts: 9
Joined: Tue Oct 02, 2007 6:15 am

Post by fakhrizaki »

feyd | 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]


Following is the code for page1:

Code: Select all

<?php
session_start();
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1); 

$_SESSION['var']='This value is stored in a session variable and should be carried to the page2.';
?>
<html>
<head>
<title>Page 1</title>
</head>
<body>
<?=$_SESSION['var'];?>
<br />
Click to go to <a href="page2.php">page2</a>
</body>
</html>
Code for page2:
===========

Code: Select all

<?php
session_start();

ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1); 
?>
<html>
<head>
<title>Page 2</title>
</head>
<body>
<p>Page2 :</p>
<p>
  <strong>Session Value:</strong> 
  <?=$_SESSION['var'];?>
</p>
</body>
</html>
OUTPUT OF PAGE 1:
==============

This value is stored in a session variable and should be carried to the page2.
Click to go to page2

OUTPUT OF PAGE 2:
==============

Page2 :

Session Value:
Notice: Undefined variable: _SESSION in /home/dragonf/public_html/dragonfeed/page2.php on line 15


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

Post by feyd »

The error you are currently receiving suggests the session didn't start as $_SESSION would be defined if it were.
fakhrizaki
Forum Newbie
Posts: 9
Joined: Tue Oct 02, 2007 6:15 am

Post by fakhrizaki »

Thanks,

But as far as the code is concerned everything is fine. session_start() is also there and the permissions to the session save path is also OK. Then what could be the reason. I am amazed because sessions were working fine previously and suddenly it stopped working :(

What do you reckon?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Code: Select all

var_dump($a = ini_get('session.gc_maxlifetime'));
Hopefully that doesn't print 0.

Or what about this, to check if the file exists.

Code: Select all

echo file_exists(session_save_path() . DIRECTORY_SEPARATOR . 'sess_' . session_id()) ? 'session file exists' : 'session file does not exist';
Or, maybe you don't have cookies enabled and your session.use_only_cookies is set to false.

Code: Select all

var_dump($a = ini_get('session.use_only_cookies'));
Last edited by s.dot on Sat Oct 06, 2007 11:59 am, edited 1 time in total.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I suspect it may be more simple. If this is being run on an Apache server, create an .htaccess file in the file's directory. In it, set error_reporting to E_ALL and display errors on. This way you don't have to set it in your files... and certain errors will display, because E_ALL in the code misses some errors which happen before the code begins parsing. http://php.net/configuration.changes for more details.

Rerun these scripts afterward. I suspect there's whitespace before the beginning of the code in the second file.
Post Reply