开发者

php setting/getting cookie same page

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开发者_C百科 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.


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/

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜