dstnlhhv791576 2014-06-21 01:34
浏览 84

会话cookie不会持久存储在本地页面,远程服务器上?

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).

  • 写回答

1条回答 默认 最新

  • dongle7637 2014-06-21 02:45
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用