检测是否在PHP中启用了cookie [英] Detect if cookies are enabled in PHP

查看:173
本文介绍了检测是否在PHP中启用了cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检测页面上的用户是否启用了Cookie.以下代码执行检查,但是,我不知道如何将用户重定向到他们来自的页面.

I am trying to detect if a user on my page has cookies enabled or not. The following code performs the check, but, I have no idea on how to redirect the user to the page they came from.

该脚本启动一个会话,并检查它是否已经检查过cookie.如果没有,它将重定向用户到测试页面,并且由于我在第一页中调用了session_start(),因此如果用户代理启用了cookie,我应该会看到PHPSESSID cookie.

The script starts a session and checks if it has already checked for cookies. If not, it redirects the user to a test page, and since I had called session_start() in the first page, I should see the PHPSESSID cookie if the user agent has cookies enabled.

问题是,可以从我网站的任何页面调用该脚本,并且我将不得不将其重定向回其选定的页面,例如index.php?page = news& postid = 4.

The problem is, ths script might be called from any page of my site, and I will have to redirect them back to their selected page, say index.php?page=news&postid=4.

session_start();
// Check if client accepts cookies //
if (!isset($_SESSION['cookies_ok'])) {
    if (isset($_GET['cookie_test'])) {
        if (!isset($_COOKIE['PHPSESSID'])) {
            die('Cookies are disabled');
        } else {
            $_SESSION['cookies_ok'] = true;
            header(-------- - ? ? ? ? ? -------- -);
            exit();
        }
    }
    if (!isset($_COOKIE['PHPSESSID'])) {
        header('Location: index.php?cookie_test=1');
        exit();
    }
}

推荐答案

我认为使一个文件集cookie并重定向到另一个文件更好.然后,下一个文件可以检查该值并确定是否启用了cookie.参见示例.

I think its better to make one file set cookie and redirect to another file. Then the next file can check the value and determine if cookie is enabled. See the example.

创建两个文件cookiechecker.phpstat.php

// cookiechecker.php
// save the referrer in session. if cookie works we can get back to it later.
session_start();
$_SESSION['page'] = $_SERVER['HTTP_REFERER'];
// setting cookie to test
setcookie('foo', 'bar', time()+3600);
header("location: stat.php");

stat.php

<?php if(isset($_COOKIE['foo']) && $_COOKIE['foo']=='bar'): 
// cookie is working
session_start();
// get back to our old page
header("location: {$_SESSION['page']}");
else:            // show the message ?>
cookie is not working
<? endif; ?>

在浏览器中加载cookiechecker.php,它将告诉cookie is working.用curl之类的命令行调用它.会说,cookie is not working

Load cookiechecker.php in browser it'll tell cookie is working. Call it with command line like curl. It'll say, cookie is not working

这是一个文件解决方案.

Here is a single file solution.

session_start();

if (isset($_GET['check']) && $_GET['check'] == true) {
    if (isset($_COOKIE['foo']) && $_COOKIE['foo'] == 'bar') {
        // cookie is working
        // get back to our old page
        header("location: {$_SESSION['page']}");
    } else {
        // show the message "cookie is not working"
    }
} else {
    // save the referrer in session. if cookie works we can get back to it later.
    $_SESSION['page'] = $_SERVER['HTTP_REFERER'];
   // set a cookie to test
    setcookie('foo', 'bar', time() + 3600);
    // redirecting to the same page to check 
    header("location: {$_SERVER['PHP_SELF']}?check=true");
}

这篇关于检测是否在PHP中启用了cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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