-2

I have a page on my local computer with a script that interacts with a remote server. For every request, it gets a different PHPSESSID value. Does this have something to do with the page not being associated with a domain? Is there a way to solve this, perhaps maybe manually setting the PHPSESSID or something in php.ini?

Look, I don't know where the downvotes are coming from or the request to close, either. Just seems like trolling to me. This is not a coding error. This is a server error. Code will do nothing. Seriously, as plain and standard as you can be, like:

$.post('http://example.com/server.php',{'stuff':'data'},function(){

});
// PHPSESSID is 545ty789gh78rhr

Then, when I call it again:

$.post('http://example.com/server.php',{'stuff2':'data'},function(){

});
// PHPSESSID is fnunurhf894fh

And then, on the php page:

session_start();
if(isset($_POST['stuff']) $_SESSION['variable'] = '123';

if(isset($_POST['stuff2']) echo $_SESSION['variable']; // Returns nothing, as the PHPSESSID cookie value has changed

Thus, I'm thinking it's either in php.ini or has something to do with how php handles session cookies with no http domain (just a local script accessing the server). I'm wondering if there's something in php.ini that would solve it or perhaps if there's a way to manually set the PHPSESSID's (if there's no way through php.ini).

JVE999
  • 3,327
  • 10
  • 54
  • 89
  • On my local computer with an ajax call that uses php sessions. Pretty standard. No code needed. The cookie changes with each ajax call. Like specifically, `C:\website\page.html`. I don't understand where the ambiguity is coming from. – JVE999 Jun 21 '14 at 01:45
  • Thanks. I'm not sure how. I suppose it might make it clearer. – JVE999 Jun 21 '14 at 02:35

1 Answers1

1

I think your issue is that cookies are stored on a per domain level. Your browser is operating on localhost, and the server is operating on example.com. So when the first ajax request is made, the browser gathers all the localhost cookies and sends them along. The server finds no example.com PHPSESSID cookie, so it creates one and starts a new session. When the request comes back to the browser a cookie is set for example.com. When the second request is made the browser looks for any cookies it has for localhost, finds none, and repeats the process.

Cross domain ajax requests are not allowed by default. You may be able to solve the problem by trying a jsonp request. See jQuery's documentation for more info.

  • That's exactly what I was thinking. Considering `$.post` is really just an alias of `$.ajax`, I don't think that would work. However, what you said confirms what I thought. The only way I could find so far is to manually set the session id, using the first note on [this page](http://www.php.net//manual/en/function.session-id.php). I also think that [this variable](http://www.php.net/manual/en/session.configuration.php#ini.session.cookie-domain) in `php.ini` might be an answer, but I'm not sure. That would be ideal as manually setting the ids is a bit of a hassle. – JVE999 Jun 21 '14 at 14:20
  • I think I might have just found the reason actually! It's a Chrome issue, http://stackoverflow.com/questions/335244/why-does-chrome-ignore-local-jquery-cookies I tried to manually send the last session id and then realized I have no cookies at all. – JVE999 Jun 21 '14 at 14:39