我可能在这里忽略了一些非常明显的东西。
注释有助于解释任何库特定的代码。
public function areCookiesEnabled() {
$random = 'cx67ds';
// set cookie
cookie::set('test_cookie', $random);
// try and get cookie, if not set to false
$testCookie = cookie::get('test_cookie', false);
$cookiesAppend = '?cookies=false';
// were we able to get the cookie equal ?
$cookiesEnabled = ($testCookie === $random);
// if $_GET['cookies'] === false , etc try and remove $_GET portion
if ($this->input->get('cookies', false) === 'false' AND $cookiesEnabled) {
url::redirect(str_replace($cookiesAppend, '', url::current())); // redirect
return false;
}
// all else fails, add a $_GET[]
if ( ! $cookiesEnabled) {
url::redirect(url::current().$cookiesAppend);
}
return $cookiesEnabled;
}
首先,我想要一种简单的方法来检查是否启用了 cookie。我实现了这一点,但如果没有 cookie,?cookies=false
URL 中就会出现丑陋的情况。
没关系,但是如果您重新加载页面并且确实再次启用了 cookie,我想重定向用户,因此它在 URL 中被剥离?cookies=false
(允许该方法重新检查并了解现在启用了 cookie 。)。