Tuesday, January 8, 2013

Empty $_SESSION after PHP redirect using GET or CURL (not browser)

Error

Empty $_SESSION after PHP redirect between 2 scripts, the first script is called using GET or CURL (not browser)

Steps

1- Call "script1" using GET or CURL
2- "Script1" starts/fills session X, does some logic, redirect to "Script2"
3- In "Script2", print_r($_SESSION) is empty

Solution

I found that the SESSION in "Script2" is a NEW session, let's call it:Session Y , since Session X was not transferred in HTTP header in the redirect

So it seems that the SESSION information transfer in headers was being done by the browser and it is not done by GET or CURL command line, 

So we need to do it instead


Step 1:

In Script #1, change the redirection to be 

header("Location:$targetPage?".SID);

SID is a constant containing the following

PHPSESSID=oilq46ge4e2f76774qcg8gi3d1


Step 2:

In script #2, start the script with the following

session_id($_GET['PHPSESSID']);
session_start();


So you are telling the script to load Script1's session

That's it


Final note 

You may have a different issue too, check this post

http://karim-ouda.blogspot.com/2012/04/php-session-empty-after-header.html


Keywords

Redirect PHP command line new $_SESSION  GET session_id start_session browser PHPSESSID cookie  CURL  Set-Cookie HTTP header

No comments:

Post a Comment