是否可以检查用户是否启用了PHP的PHP没有页面重新加载? [英] Is it possible to check if user has cookies enabled in php without a page reload?

查看:121
本文介绍了是否可以检查用户是否启用了PHP的PHP没有页面重新加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,下面的代码似乎不工作,即使cookie实际设置,直到我刷新页面。

For example, code such as the following doesn't seem to work, even if the cookie is actually set, until I refresh the page.

setcookie("cookies","1", time()+ 86400,"/" ); 
if (!isset($_COOKIE["cookies"])) {  
   $cookies = "foobar";
}



我试图在我的网站上写一个准确的唯一用户计数器基于检查用户是否在计算机上设置了Cookie,如果他们没有设置cookie,它会+1,并设置cookie。这里的问题是,没有cookie的用户只会在每个页面加载+1,所以自然我只想为那些已启用cookie的人运行此代码。

Im trying to write an accurate "unique user" counter on my site, which works based on checking to see if a user has a cookie set on the computer, if they don't, it does +1, and sets the cookie. The problem here is that, the users without cookies will just +1 on every page load, so naturally I only want to run this code for those who have cookies enabled.

上面的cookie检查程序在第一页加载时总是返回$ cookies =foobar,不管用户是否打开或关闭cookie。因此,如果用户只查看网站的1页面,他们的访问将不会被计数器注册。

The cookie checker above however will always return $cookies = "foobar" on the first page load, regardless of users having cookies on, or off. So if a user just views 1 page of the site, their visit won't be registered by the counter.

推荐答案

您无法通过检查设置值立即检查其是否存在Cookie,因为Cookie不会发送到浏览器,直到

You cannot immediately check for its existence of cookies by checking the set value, because cookies are not sent to the browser until the next round trip to the client.

您可以做的一件事是在页面上设置cookie,然后在图像脚本上检查它。

One thing you could do is set the cookie on the page, and then check it on an image script.

还有其他解决方法。

这篇关于是否可以检查用户是否启用了PHP的PHP没有页面重新加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆