简体   繁体   English

PHP cookie问题,适用于Firefox而不是其他浏览器

[英]PHP cookies problem, works in Firefox not in other browser

I've a problem with setting cookies in php. 我在php中设置cookie有问题。 I've to say that I'm not very experienced with php, so maybe is a very stupid problem. 我要说我对php不是很有经验,所以也许是一个非常愚蠢的问题。

I've an ajax rating system that should check a cookie to see if the the photo has already been voted. 我有一个ajax评级系统应该检查一个cookie,看看这张照片是否已被投票。

The page called with ajax check for the cookie, add the id of the photo you are voting to it and call this function: 使用ajax检查cookie的页面,添加您投票给它的照片的ID并调用此函数:

   setcookie("Name", $cookie, time()+(60*24*365), "/",  $_SERVER['HTTP_HOST'], 0); 

The page that display the photo also call the cookie 显示照片的页面也会调用cookie

   $cookie = $_COOKIE['Name'];

and check to see if you have already voted. 并检查你是否已经投票。

A problem may be the fact that the ajax page is in a different directory than the page that display the photo. 问题可能在于ajax页面与显示照片的页面位于不同的目录中。

The page that display the photo is in the root directory, the page that cast the vote is in /ajax/vote.php 显示照片的页面位于根目录中,投票的页面位于/ajax/vote.php中

The voting system works, before I was checking the IPs, but know i need to check the cookies. 在我检查IP之前,投票系统有效,但知道我需要检查cookie。

It work in Firefox without any problem, but when I've started testing on IE and Safari it seem they don't see the cookie. 它在Firefox中运行没有任何问题,但是当我开始在IE和Safari上进行测试时,似乎他们看不到cookie。

I've checked with IECookieViewer and when I cast a vote the cookie is created allright, but when I go back to the page, it look like the page don't find the cookie. 我已经使用IECookieViewer进行了检查,当我投票时,cookie已经创建了,但是当我回到页面时,它看起来像页面找不到cookie。 Also if I cast another vote the cookie is replaced with a new one. 此外,如果我再次投票,则将cookie替换为新的。

Sorry for the bad english, I hope the problem is understandable 对不起英语不好,我希望这个问题是可以理解的

PS Forgot to point something that might be related to the problem. PS忘了指出可能与问题有关的事情。 The page is inside an iframe. 该页面位于iframe中。

Check the cookie settings of the other browsers and if they're set to block all or empty on exit. 检查其他浏览器的cookie设置,如果它们设置为阻止全部或退出时为空。

If the cookies work in one browser, but not another, you will need to make sure that the other browser is letting you set cookies in the first place. 如果cookie在一个浏览器中工作,而不是在另一个浏览器中工作,则需要确保其他浏览器首先允许您设置cookie。

Sometimes it will look like you can create the cookie, but then it will disappear or be deleted with each page reload. 有时它看起来像你可以创建cookie,但随后每个页面重新加载它将消失或删除。

Cookies from an iframe 来自iframe的Cookie

It's also possible that because you're setting the cookies in an iframe, that the browsers may view it as a third-party cookie and reject it unless explicitly set out in the browser preferences to allow third-party cookies . 也有可能因为您在iframe中设置了Cookie,浏览器可能会将其视为第三方Cookie并拒绝它,除非在浏览器首选项中明确规定允许第三方Cookie

In that case you would need a compact privacy policy (or a compact P3P header ) on the pages from where you're trying to set the cookies from. 在这种情况下,您需要在尝试设置cookie的页面上使用紧凑的隐私策略(或紧凑的P3P标头 )。

For PHP, you would add this as your header for the page setting the cookie: 对于PHP,您可以将其添加为设置cookie的页面的标题:

header('P3P:CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"'); 

I've had a similar problem in the past where cookies would be set by firefox but not IE if I was working with a local network server (192.168.0.102 or something like that) but when I ported it all over to the public server with a domain name it worked fine. 我曾经遇到类似的问题,如果我使用的是本地网络服务器(192.168.0.102或类似的东西),那么cookie将由firefox设置但不是IE,但是当我将它全部移植到公共服务器时一个域名它工作得很好。

IE might have some kind of strange rules about cookies and domain names. IE可能对cookie和域名有一些奇怪的规则。 Not sure if this helps in your case. 不确定这对您的情况是否有帮助。

The IFRAME is the reason your cookies are being lost. IFRAME是您丢失cookie的原因。 Your PHP session id cookie is also being lost and a new session is created on each request, therefore losing track of previous state. 您的PHP会话ID cookie也会丢失,并且会在每个请求上创建一个新会话,因此会丢失以前的状态。

While P3P headers may help, client browsers can still be set to disallow third party cookies and P3P can't override that. 虽然P3P标头可能有所帮助,但客户端浏览器仍然可以设置为禁止第三方cookie,而P3P无法覆盖它。 Steve Gibson of Security Now! 现在安全的史蒂夫吉布森 podcast fame repeatedly recommends to enable blocking third party cookies because it stops things like tracking cookies. 播客名称反复建议启用阻止第三方Cookie,因为它会停止跟踪Cookie等内容。

What you need to do is switch to form-based session id. 您需要做的是切换到基于表单的会话ID。 PHP makes this easy by supplying the "PHPSESSID" form variable in either querystring or hidden input form variable with the value of session_id() . PHP通过在查询字符串或隐藏的输入表单变量中提供“PHPSESSID”表单变量并使用session_id()的值来session_id() Or if you want to be portable, check the value of ini_get('session.name') incase the web server is configured with a custom session variable name, different than "PHPSESSID". 或者,如果您想要可移植,请检查ini_get('session.name')的值, ini_get('session.name') Web服务器配置了自定义会话变量名称,不同于“PHPSESSID”。

Are you setting the cookie on the exact same URL? 您是否在完全相同的URL上设置cookie? If you're using $_SERVER['HTTP_HOST'] then one browser may set it for http://example.com while another may set it on http://www.example.com . 如果您使用的是$_SERVER['HTTP_HOST']则一个浏览器可能会将其设置为http://example.com,而另一个浏览器可能会将其设置为http://www.example.com If you visit the alternate version the cookie won't be set. 如果您访问备用版本,则不会设置cookie。

To quote the PHP docs : 引用PHP文档

The domain that the cookie is available. Cookie可用的域。 To make the cookie available on all subdomains of example.com then you'd set it to '.example.com'. 要在example.com的所有子域上提供cookie,您需要将其设置为“.example.com”。 The . 的。 is not required but makes it compatible with more browsers. 不是必需的,但使其与更多浏览器兼容。 Setting it to www.example.com will make the cookie only available in the www subdomain. 将其设置为www.example.com将使cookie仅在www子域中可用。

So try setting '.yoursite.com' as the domain. 因此,请尝试将“.yoursite.com”设置为域名。

Other things to try: 其他尝试:

  • $name is the only required parameter in the setcookie function - try skipping everything after $expire and see if it works. $ name是setcookie函数中唯一必需的参数 - 尝试在$ expire之后跳过所有内容并查看它是否有效。
  • Try setting an $expire value a long way into the future. 尝试在未来很长一段时间内设置$ expire值。 Maybe there is something on your computer or the server causing a time issue? 也许您的计算机或服务器上有什么东西导致时间问题?
  • Have you checked in your browser if the cookie is definitely being set? 您是否在浏览器中检查过cookie是否已被设置? There should be a way to list every cookie set. 应该有一种方法来列出每个cookie集。 Set the cookie and check your browser. 设置cookie并检查您的浏览器。

problema en cookies para ie SOLUCIONADO: problem at cookies for ie SOLVED: problema en cookies para ie SOLUCIONADO:cookies的问题,即已解决:

Es SIMPLE, Cuando declaren la cookie y escriben en el EXPIRE: time()+60*60*24*30, cambien los 60's por 120 asi: time()+120*120*24*30. Es SIMPLE,Cuando声明la cookie y escriben en el EXPIRE:time()+ 60 * 60 * 24 * 30,cambien los 60's por 120 asi:time()+ 120 * 120 * 24 * 30。 A m'i me funcion'o a la perfeccion. 我是一个完美的人。

Its simple, when you declare the cookie, at the expire time "time()+60*60*24*30", change los 60's by 120 so: time()+120*120*24*30. 当你声明cookie时,它很简单,在到期时间“time()+ 60 * 60 * 24 * 30”,将los 60改为120,所以:time()+ 120 * 120 * 24 * 30。 It worked for me. 它对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM