0

I have a main page that starts a PHP session, which creates session ID #1. I check this with session_id(). I want to access these session variables from a PHP AJAX script. To test this i just simply execute:

session_start();
echo session_id();

from this AJAX script. It returns a different AJAX session ID.

I know I can pass the session ID from the main page to the AJAX script. Is there another way to make the AJAX script open the same session by default?

For clarification I am doing this via javascript, more specifically it is the add/edit script for JQGrid.

3
  • how are you making a call? If it is via JS, it should have the same session id. If it is flash, it is different. Commented Jan 11, 2012 at 15:47
  • This is to do with the scope of your session cookie. Obviously the location called by your AJAX call falls outside the scope of your session cookie - to fix this, you either need to widen the scope of the cookie or change the target location so that it falls within the existing scope. Alternatively, you could manually pass the session ID as a GET or POST parameter in the AJAX request. Further reading on cookie scope here. Commented Jan 11, 2012 at 15:47
  • @DaveRandom - Thanks for the info about the cookie scope. I think this must be the problem. The session variable is defined in the web root -> index.php file, and the AJAX script is in a sub folder of the web root. I will try to widen the scope. I think you could post your comment as an answer. Commented Jan 11, 2012 at 19:00

1 Answer 1

1

This is simple.

in ajax, post your session_id to the ajax php.

in ajax php set it like: session_id($_REQUEST['sid'])

then you are in the same session

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.