为什么这个 HTTP 请求不断循环?
我可能在这里忽略了一些非常明显的事情。
注释是为了帮助解释任何库特定的代码。
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 的情况下,URL 中会有一个丑陋的 ?cookies=false
。
没关系,但是如果您重新加载页面并再次启用 cookie,我想重定向用户,以便它剥离 URL 中的 ?cookies=false
(允许该方法重新检查和学习现在 cookie 已启用。)。
I'm probably overlooking something really obvious here.
Comments are in to help explain any library specific code.
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;
}
Firstly, I wanted an easy way to check if cookies were enabled. I achieved this, but in the event of no cookies, there was an ugly ?cookies=false
in the URL.
That was OK, but then if you reloaded the page and did have cookies enabled again, I wanted to redirect the user so it stripped off ?cookies=false
in the URL (allowing the method to recheck and learn that cookies now are enabled.).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
$cookiesEnabled = ($testCookie === $random);
之后,有4种情况:$cookiesEnabled
为true,$_GET['cookies'] = == 'false'
为 true$cookiesEnabled
为 true,$_GET['cookies'] === 'false'
为 false$cookiesEnabled
为 false,$_GET['cookies'] === 'false'
为 true$cookiesEnabled
为 false,$_GET['cookies' ] === 'false'
为 false情况 1 由第一个
if
块处理。return
语句旨在处理情况 2 和 3;第二个if
块仅用于处理情况 4,但它同时捕获情况 3 和 4。在情况 3 中,URL 已经具有?cookies=false
,但由于$cookiesEnabled
为 false,我们重定向以添加?cookies=false
,然后循环回到情况 3。After
$cookiesEnabled = ($testCookie === $random);
, there are 4 cases:$cookiesEnabled
is true and$_GET['cookies'] === 'false'
is true$cookiesEnabled
is true and$_GET['cookies'] === 'false'
is false$cookiesEnabled
is false and$_GET['cookies'] === 'false'
is true$cookiesEnabled
is false and$_GET['cookies'] === 'false'
is falseCase 1 is handled by the first
if
block. Thereturn
statement is intended to handle cases 2 and 3; the secondif
block is intended to handle only case 4, but it catches both case 3 and 4. In case 3, the URL already has?cookies=false
, but since$cookiesEnabled
is false, we redirect to add?cookies=false
, and cycle back into case 3.您一定遗漏了一些东西,因为该代码中没有循环。如果您的意思是浏览器正在循环(例如获取连续重定向),那么我建议安装实时 HTTP 标头 扩展至 Firefox,并观察浏览器和服务器实际上彼此所说的内容。在上面的代码片段中放入一些日志记录代码也可能具有指导意义。
评论更新:
那么我真的建议在
if
中放入 print 语句,这样您就可以看到正在经历哪些语句以及各个值是什么。显然某些东西并没有按照您想象的方式设置,所以现在您需要找出它实际上是什么。我多次遇到的一件事是代码本身没问题,但有一个 .htaccess 文件对您不利,因此请仔细检查从 DOCUMENT_ROOT 开始的任何目录中的任何 .htaccess 文件。
You must be leaving something out since there is no loop in that code. If you meant that the browser is looping (e.g. getting continuous redirects), then I recommend installing the Live HTTP Headers extension to Firefox and watch what the browser and server are actually saying to each other. Putting in some logging code in the snippet above might also be instructive.
Update for comment:
Then I really recommend putting in print statements inside the
if
s so you can see which ones you're going through and what the various values are. Clearly something is not getting set the way you thought it would be, so now you need to find out what it actually is.One thing I have encountered several times is that the code itself is OK, but there is a .htaccess file that is working against you, so go double check any .htaccess files in any of the directories, starting from DOCUMENT_ROOT.