php设置/获取cookie同一页面

发布于 2024-11-29 17:45:28 字数 463 浏览 2 评论 0原文

我在设置和获取 COOKIES 时遇到了一些问题。我在 iframe 外部有一个页面,我在其中设置了 cookie,然后重定向到 facebook,在那里我获取了之前设置的 COOKIE 并将其存储在数据库中。这个用户路径工作正常,除了用户可以直接到达 Facebook 页面(选项卡),如果没有设置,我需要在那里设置一个 cookie。

我知道在某些浏览器中设置 COOKIE 时应该存在 P3P 策略,因此我使用: header('P3P:CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"');它对 IE 起作用,但我注意到问题不仅仅与 IE 有关。

一个可能的问题可能是我设置并尝试直接在同一页面上获取COOKIE,而设置COOKIE后没有刷新页面??? iframe 会影响通过 PHP 设置 COOKIES 的方式吗?

有什么想法吗?

提前致谢。

I ve been having some problems with setting and getting COOKIES. I have a page outside an iframe where i set a cookie and then redirect on to facebook, where i get the COOKIE previously set and store in a DB. This user path works fine, except that a user can arrive to the facebook page (tab) directly, where i need to set a cookie there if one is not set.

I know that in certain browsers a P3P policy should be present when setting a COOKIE so i use:
header('P3P:CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"'); which does its job for IE, but i have noticed that the problem is not only IE related.

One possible problem might be that i set and try to get the COOKIE on the same page directly without refreshing the page after the COOKIE is set ??? Whould the iframe affect how COOKIES are set through PHP ?

Any thoughts?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感悟人生的甜 2024-12-06 17:45:28

Cookie 在所有非 IE 浏览器中应该可以正常工作。只要您尚未发送标头,您就应该能够检查 cookie 并将其设置在同一组 PHP 代码中。服务器将收到一个请求,其中可能包含大量标头,其中可能包括您的 cookie。

然后,您的 PHP 代码应该能够检查这些 cookie,并设置大量标头以发送回用户的浏览器。然而,在完成标头设置之前,很容易意外地将一些内容发送回用户的浏览器,在这种情况下,在此之后设置 cookie 将不起作用。例如,如果您将 PHP 结束标记 ?> 放入文件中,然后添加一个换行符或一个空格,则会在代码运行时发送回用户的浏览器,这将发回响应标头,这意味着在此之后它们将无法修改。

不过,您应该会收到“标头已发送”错误。

尝试运行:
http://www.php.net/manual/en/function.headers -sent.php

http://www.tech-recipes.com/rx/1489/solve-php-error-cannot-modify-header-information-headers-already-sent/

Cookies should work fine in all non-IE browsers. And you should be able to check for a cookie and set it in the same set of PHP code, as long as you haven't already sent headers. The server will receive a request, with a load of headers possibly including your cookie.

You PHP code should be able to then check these cookies, and set a load of headers to send back to the user's browser. However, it is quite easy to accidentally send some stuff back to the user's browser before you've finished setting the headers, in which case setting cookies after that point won't work. For example, if you put a closing PHP tag ?> in a file, and then a new line or a space, that will get sent back to the user's browser at the point that code is run, which will send back the response headers, meaning they can't be modified after that point.

You should be getting a "headers already sent" error though.

Try running:
http://www.php.net/manual/en/function.headers-sent.php

http://www.tech-recipes.com/rx/1489/solve-php-error-cannot-modify-header-information-headers-already-sent/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文