PHP filter_input返回已删除的cookie

时间:2014-11-03 18:20:03

标签: php cookies filter-input

据我所知(例如Remove a cookie)在PHP中删除cookie的典型过程是:

  • 使用" setcookie"设置过去的到期时间,以便浏览器在下一页重新加载时删除cookie。
  • call" unset"从" $ _ COOKIE"中删除Cookie超全局。

但是当我现在尝试使用" filter_input"来访问cookie时它仍然是固定的。在我看来," filter_input"没有访问" $ _ COOKIE"超全球但使用其他内部缓冲区。

有没有办法我可以永久删除cookie,以便即使" filter_input"不再回来了吗?

感谢您的帮助

这是我的示例代码:

<?php

// assembles the current URL
function currentUrl() {

    $https = filter_input(INPUT_SERVER, 'HTTPS', FILTER_VALIDATE_BOOLEAN);
    $protocol = $https ? 'https://' : 'http://';
    $serverName = filter_input(
            INPUT_SERVER, 'SERVER_NAME', FILTER_SANITIZE_STRING);
    $port = isset($_SERVER['SERVER_PORT'])
            ? ':' . filter_input(
                    INPUT_SERVER, 'SERVER_PORT', FILTER_VALIDATE_INT)
            : '';
    $requestURI = filter_input(
            INPUT_SERVER, 'REQUEST_URI', FILTER_SANITIZE_ENCODED);
    $pageURL = $protocol . $serverName . $port . $requestURI;
    return $pageURL;
}


const COOKIE_NAME = 'foo';
const COOKIE_VALUE = 'bar';

// ensure that we have our cookie set
$cookie = filter_input(INPUT_COOKIE, COOKIE_NAME, FILTER_SANITIZE_ENCODED);
if (empty($cookie)) {
    $expires = time() + 60*60;
    setcookie(COOKIE_NAME, COOKIE_VALUE, $expires);
    header('Location: ' . currentUrl());
    die();
}

// remove the cookie from $_COOKIE
unset($_COOKIE[COOKIE_NAME]);
print_r($_COOKIE);      // this won't list our cookie
echo '<br/>';

$cookie = filter_input(INPUT_COOKIE, COOKIE_NAME, FILTER_SANITIZE_ENCODED);
echo $cookie;           // this will print the cookie content

0 个答案:

没有答案