开发者

AJAX + PHP: Session timeout

开发者 https://www.devze.com 2022-12-10 09:24 出处:网络
Could anyone tell me why my $_SESSION variables are not being carried over to my other PHP page that is called with ajax.

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!


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.


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!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消