PHP if(设置cookie重定向循环 [英] PHP if(cookie is set Redirect loop
问题描述
因此,我在使用非常简单的PHP时遇到了麻烦,因为我不擅长使用它。所以这是代码:
So, I'm having trouble with quite simple PHP, as I am not adept in anyway with it. So here's the code:
<?php
if(!isset ($_COOKIE['cookie'])) {
header("Location: index.html");
} else {
header("Location: index2.php");
?>
它在HTML文档的顶部,在其他HTML之前,因为我听说标头会否则工作(该陈述本身可能证明我的无知)。但基本上,我有一个同意页面,您必须先同意才能继续访问该网站,但该页面不被视为我的索引文件。因此,我需要此重定向来检测是否存在由Agreement.php设置的cookie,并且我认为此语法正确,但似乎不起作用。我使用了echo
有任何解决方法吗?谢谢您。
It's at the top of an HTML document, before any other HTML because I've heard header won't work otherwise (that statement could prove my ignorance itself). But basically, I have an agreement page you must agree to before continuing to the site, but it's not considered my index file. So I need this redirect to detect the if the cookie that is set by the agreement.php exists or not, and I assume that this syntax is correct, but it seemingly doesn't work. I used an echo "
Any ideas on how to fix? Thank you in advance.
推荐答案
尝试使用以下代码:
index.php应该是此文件(文件的开头到结尾)
index.php should be this (start of file to end)
<?php
if (!isset($_COOKIE['cookie']))
{
header('Location: http://www.example.com/index2.php');
exit;
}
?>
<!DOCTYPE html>
...rest of your HTML code
这篇关于PHP if(设置cookie重定向循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!