新的会话cookie在下次服务器请求之前未提供(new Session cookie not served up until next server request)

编程入门 行业动态 更新时间:2024-10-18 12:20:34
新的会话cookie在下次服务器请求之前未提供(new Session cookie not served up until next server request)

嗨,大家好,所以我在Codeignitor开发一个应用程序。 内容基于三个选择标签进行更新。 填充地点的第一个城市为该城市选择标记,同时使类别选择标记可见。 已经成功设置了一堆ajax调用。 内容根据ajax调用更新。 现在我点击内容链接查看内容。 成功...但是当我点击浏览器的按钮时,我搞砸了选择标签。 所以我意识到我需要一个会话变量,这样当用户点击浏览器的按钮时,我可以让他保持在同一个选择标签阶段。 我能够成功存储会话让我们暂时说出城市。 然后我用了

$(document).ready(function () { $city="<?php echo $this->session->userdata('city'); ?>"; alert($city); });

检查一切是否正常。 现在的问题是洛杉矶(洛杉矶):1伦敦:2

方案1:我选择LA然后查看加载内容点击查看内容然后点击浏览器按钮然后得到空白警告框。 但是,如果我重新加载,那么我会得到一个警报

方案1之后的方案2:我选择洛杉矶然后伦敦视图内容然后回来,我在警告框中看到1 。 我重新加载页面,我在alertbox中看到2所以我挖到了这个链接 ,发现了问题的原因......看起来像刷新的会话只在下一个服务器请求上提供。 你可以看到我的问题,我想要刷新信息,以便我可以让我的用户保持在他们中断的状态。

关于如何解决这个问题的任何建议?

Hi Guys so i am developing an app in Codeignitor. content is updated based on three select tags. First one city which populates locality select tag for that city and at the same time makes category select tag visible. Have successfully setup for bunch of ajax calls. content is updated according to the ajax call. Now I click on the content link to view the content. success... but when i hit back button of the browser I get messed up select tags. So then I realized that I need a session variable so that when user hits back button of browser I can keep him at the same select tag stage. I am able to successfully store the session lets say city for the moment. then I used

$(document).ready(function () { $city="<?php echo $this->session->userdata('city'); ?>"; alert($city); });

to check if everything is ok or not. Now the problem is that Los Angeles(LA):1 London:2

scenario 1: I select LA then look at loaded content click to view the content then hit back button of browser then get blank alert box. But if i reload then I get alert with 1 in it

scenario 2 after scenario 1: I select LA then London view content then come back and I see 1 in alert box. I reload the page i see 2 in alertbox So I dug into this linkand found the cause of the problem... looks like refreshed session is only served on the next server request. AS you can see my problem i want that refreshed info so that i can keep my users at the same state where they left off.

Any suggestions on how I can solve this problem?

最满意答案

这就是cookie的工作原理。 发送请求来自客户端。

解决方法是手动设置$_COOKIE变量:

setcookie('yourcookie', 'something', time()+3600, '/', 'http://example.com', false, true); $_COOKIE['yourcookie'] = 'something'; // yay I can now access it right away echo $_COOKIE['yourcookie'];

UPDATE

现在我觉得我误解了你的问题。 在使用浏览器后退按钮时,您似乎遇到了问题? 那是对的吗? 如果是这样,您将始终获得页面的先前状态,并且确实不会设置cookie(因为它将从浏览器缓存中检索它)。 我看到这个将永远有效的唯一方法是通过一些脚本(即JS)刷新页面,但这将是烦人的。 或者通过AJAX调用(新请求)检索cookie值到服务器。

That is how cookies work. The are send with the request from the clientside.

A workaround is to set the $_COOKIE variable manually:

setcookie('yourcookie', 'something', time()+3600, '/', 'http://example.com', false, true); $_COOKIE['yourcookie'] = 'something'; // yay I can now access it right away echo $_COOKIE['yourcookie'];

UPDATE

Now that I think I misunderstood your question. It looks like you are having an issue when using the browsers back button? Is that correct? If so you will always get the previous state of the page and the cookie will indeed not be set (because it will retrieve it from the browsers cache). The only way I see this will ever work is either by refreshing the page through some script (i.e. JS), but that will be annoying. Or retrieve the cookie value through an AJAX call (new request) to the server.

更多推荐

本文发布于:2023-07-26 00:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1268428.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:下次   服务器   cookie   Session   request

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!