要准确地回答你的问题,如果你在你的代码中创建一个功能
<?php
function cookies_are_enabled() {
setcookie('enabled', 'enabled');
return $_COOKIE['enabled'] === 'enabled';
}
?>
然后,你必须:
<?php
if (cookies_are_enabled()) {
/* Cookies related code goes here */
/* Create PHP cookie, read cookies etc */
} else {
/* Do something else */
}
?>
更新:正如评论中指出的那样。这不会直接工作。从setcookie PHP页面(我的重点):
“一旦饼干已经确定,就可以把他们的下一个页面加载与$ _COOKIE或$ HTTP_COOKIE_VARS数组访问。请注意,像$ _COOKIE这样的超全球变量可以在PHP 4.1.0中使用。 Cookie值也存在于$ _REQUEST中。'
鉴于你不能相信setcookie,我能想到的最好的办法是强制重定向。
<?php
function cookies_are_enabled() {
// if first page load
// set cookie and redirect
// if redirected check the cookie
if (isset($_GET['cookie_check'])) {
return $_COOKIE['enabled'] === 'enabled';
} else {
setcookie('enabled', 'enabled');
if (empty($_SERVER['QUERY_STRING'])) {
$url = $_SERVER['PHP_SELF'].'?cookie_check=1';
} else {
$url = $_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'].'&cookie_check=1';
}
exit(header("Location: $url"));
}
}
if (cookies_are_enabled()) {
/* Cookies related code goes here */
/* Create PHP cookie, read cookies etc */
$message = 'cookies are enabled';
} else {
/* Do something else */
$message = 'cookies are <strong>not</strong> enabled';
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Cookies!</title>
</head>
<body>
<p><?php echo $message; ?></p>
</body>
</html>
入住这http://stackoverflow.com/questions/531393/how-to-detect-if-cookies-are-disabled-is-it-possible – 2012-04-05 13:26:15