[SOLVED...]lost sessions after 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

Post Reply
rubberjohn
Forum Contributor
Posts: 193
Joined: Fri Feb 25, 2005 4:03 am

[SOLVED...]lost sessions after redirect

Post by rubberjohn »

i've just been looking at why my session data is not being retained after a redirect. however session info has been retained in other parts of my site after a redirect, why is it now being lost?

it is happening in the course of three pages

search.php to search_process.php - sessions passed ok
search_process.php to search_results.php - no sessions passed

any ideas??

thanks

rj
Last edited by rubberjohn on Tue Apr 18, 2006 11:16 am, edited 1 time in total.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Can we see the code?
rubberjohn
Forum Contributor
Posts: 193
Joined: Fri Feb 25, 2005 4:03 am

Post by rubberjohn »

the whole code is too long but the relevant bits are

on the search page

Code: Select all

if(isset($_POST['search'])){
	if(isset($_SESSION['my_search_box'])){
		 header('Location: http://www.merseysideemploymentlaw.co.u ... ocess.php'); 
	 	 exit;
	}else{
		 echo "Please choose a tag before trying to search";
	}
}

$_SESSION['my_search_box'] can then be access in search_process.php, also in search_process.php i declare

Code: Select all

$_SESSION['leaf_match'] = serialize($arr_leaf_match);
$_SESSION['sub_top_match'] = serialize($arr_topic_match);

header("Location: 
http://www.merseysideemploymentlaw.co.u ... s.php?.sid)"); 
  		
exit;
at the end of the script.

Then when I echo $_SESSION['leaf_match'] or $_SESSION['sub_top_match'] nothing is echoed and when i use session_is_registered on them it returns FALSE. I have also tried session_write_close();

is it because the session variables in search_process.php are declared just before the redirect?

thanks rj
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

if session_write_close() isn't working try

Code: Select all

$_SESSION['leaf_match'] = serialize($arr_leaf_match);
$_SESSION['sub_top_match'] = serialize($arr_topic_match);

header('Location: http://www.merseysideemploymentlaw.co.u ... ults.php?' . session_name() . '=' . session_id());
             
exit;
rubberjohn
Forum Contributor
Posts: 193
Joined: Fri Feb 25, 2005 4:03 am

Post by rubberjohn »

do i need to use that sess_id to reinitiate the sessions in search_results?

if not that doesn't work either - i think it is because the session initialisation is so close to the redirect - if a create a dummy session and place it near the redirect, it doesn't work but i put it at the top of the script it does work

rj
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Can you post the top of the script as well please?

Try replacing this:

Code: Select all

header("Location: http://www.merseysideemploymentlaw.co.u ... s.php?.sid)");
With this:

Code: Select all

header("Location: http://www.merseysideemploymentlaw.co.u ... ts.php?SID)");
rubberjohn
Forum Contributor
Posts: 193
Joined: Fri Feb 25, 2005 4:03 am

Post by rubberjohn »

no that didn't work - what do mean top of the script?

if you mean the dummy session - at the top of search_process.php

Code: Select all

$test = array("2","4","5");
$test = serialize($test);
$_SESSION['tester'] = $test;
and when this is echoed in search_results.php it works even tho. it has been redirected.

rj
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

You have to call session_start() on each one of you pages before any output is sent to the browser. Did you do that?
rubberjohn
Forum Contributor
Posts: 193
Joined: Fri Feb 25, 2005 4:03 am

Post by rubberjohn »

yes i have started the session.

as i said above if i place

Code: Select all

$test = array("2","4","5");
$test = serialize($test);
$_SESSION['tester'] = $test;
at the top of the search_process page - the session will echo on the search_results page but when I move it to just before the redirect, it doesnt echo.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Please post the code of each one of the 3 files you have and write above each code the name of the file, cause it's kinda stupid to work like this.
It's not one of these games where each one asks questions at his turn until someone knows the right answer :wink:
User avatar
ed209
Forum Contributor
Posts: 153
Joined: Thu May 12, 2005 5:06 am
Location: UK

Post by ed209 »

Not sure if this helps?

php.net
If you want to use a named session, you must call session_name() before calling session_start()
rubberjohn
Forum Contributor
Posts: 193
Joined: Fri Feb 25, 2005 4:03 am

Post by rubberjohn »

i just went through the code bit by bit and commented out everything out - found it still didnt work and then when i uncommented everything, it worked???? - so i dont really know whats going on here there must be a bug somewhere which ill have to find but thanks for your help

rj
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

maybe it was a caching problem?
Post Reply