1

Could anyone tell me why my $_SESSION variables are not being carried over to my other PHP page that is called with ajax.

All pages have session_start();

It works on my local machine, but when I upload it to my server, it doesn't work, and on refresh it takes me back to the login screen...

EDIT:

The session variables saved once a user logs in

$_SESSION['user_id'] = $user_id;
$_SESSION['username'] = $user_email;
$_SESSION['name'] = $un; 
$_SESSION['login_times'] = $login_time;
$_SESSION['profile_pic'] = $profile_pic;

And when the ajax script calls the other PHP:

 session_start();

 $user_id = $_GET['id'];
 $newsfeed_id = $_GET['nf_id'];
 $comment = $_GET['comment'];

 $conn = mysql_connect('localhost', 'admin', 'root') or die(mysql_error());
 mysql_select_db('main') or die(mysql_error());

 // insert new comment

 $query = "INSERT INTO newsfeed_comments ".
 "VALUES ('', '{$_SESSION['user_id']}', '{$comment}', '{$newsfeed_id}')";

  mysql_query($query) or die(mysql_error());

But nothing is returned in the response text, and the values of $_SESSION['username'] has been unset, and i get redirected back to the login.

Does anyone know what the problem is?

Thanks!

3
  • What exactly is not carried over, some session data or the whole session? Please be more specific. Commented Oct 31, 2009 at 22:08
  • Is session_start called in login script? You should trace if session id passed (try debug proxy Fiddler) Commented Oct 31, 2009 at 22:16
  • yes it is. session_start(); is on top of every page. Session work everywhere, they only fail when i call an AJAX request Commented Oct 31, 2009 at 22:18

3 Answers 3

3

All cookies (including session cookies) have a path parameter that defines the prefix for which the cookie will be valid. If you want the session to be valid for the whole domain just set it to "/".

Session cookie parameters can be defined using session_set_cookie_params.

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

Comments

0

Now that I think of it, it will probably be because you save your SESSION data for a specific domain. On your localhost you are just localhost but on a server your session might be saved only for a specific part of your site. Pass an extra param to specify domain as well.

Have you done session_start() before outputting anything on the login page?

The configuration of session is here: http://php.net/manual/en/session.configuration.php sorry, got confused you were using cookies.

Try

var_dump($_SESSION); die();

on each page to see what you have.

Also do you call header() anywhere after saving into the session?

This is a nice article debugging your situation: http://www.chipmunkninja.com/Troubles-with-Asynchronous-Ajax-Requests-g@ good luck!

3 Comments

What do you mean by that? the session data is saved when the login script, which get the user to his homepage.. from there when i comment and refresh the page, the session disappears.
how do i pass those parameters?
i don't call a header() anywhere.. on var_dump i get: array(0) { }
0

I figured out what the problem was... This is really frustrating, but it's a quick fix.

i have 3 directories

/ <- root /ajax <- for ajax php files /js <- for javascript files

I was sending requests to ajax/comment.php and it treated the directory ajax/ as a different domain. So i moved all my Ajax php files into the root directory and renamed them to ajax_File.php and edited the javascript and now it works.

This really sucks, but anyone know if there is a way to get it to work in a subdirectory, because this is really frustrating.

1 Comment

Check my answer, this should help.

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.