4

I am having a problem in accessing a session variable.I have one page lets say test.php, when i use print_r($_SESSION) here, it prints all the session data.But when i use <a href="test2.php">Redirect</a> then i am unable to access session data on test2.php i-e print_r prints empty array, even though i have session_start() at the top of my script.

Then i tried header("Location: test2.php") and now session data is accessible. But i want the page to be redirected on onClick of a button.

Please help ..

5
  • 1
    Is your PHP session id in a cookie or supposed to be in the url variable? Can you post the code of both pages? Commented Feb 16, 2012 at 9:27
  • Please add a var_dump($_COOKIES); next to the print_r of the $_SESSION. Compare the differences. Commented Feb 16, 2012 at 9:33
  • @hakre var_dump($_COOKIES); print NULL Commented Feb 16, 2012 at 9:44
  • @claude please read my comment below for code .... Commented Feb 16, 2012 at 10:05
  • Please try again without the S, $_COOKIE, my fault. It should display the session id cookie. If not, the session got lost and session_start will create a new session. Commented Feb 16, 2012 at 10:44

3 Answers 3

2

Make sure you have session_start(); in the head of both files and you're not browsing in a private browsing mode.

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

7 Comments

Have you got cookies enabled in your browser?
Could you provide samples of both files? session_start(); should be at the top of test1 and test2 (and any other files you wish to have an active session in).
TEST1.php session_start(); $order_id = $_GET['order_id']; <a href="orders.php?order_id=<?php echo $enc->encrypt($order_id);?>">Purchase</a>
orders.php session_start(); print_r($_SESSION);
Where is $_SESSION['user_id'] being set initially? It looks like you're using a framework. It may store sessions differently.
|
0

I was using the path http://localhost/project/orders.php in href then i changed it to just orders.php and it worked

Cheers

Comments

-1

Sometimes, accessing with http or https or http://www can make the difference of variables in session being accessed or not. Please browse through all in order to be certain, as sometimes saved urls are with www whereas the session is created with simple http. It matters in session accessibility.

1 Comment

Please add some explanation to your answer such that others can learn from it. I don't think that the session is tied to the protocol that is being used

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.