需要cookie来查看网站,或者被重定向到不同的页面(PHP)


Require cookie to view website, or be redirected to different page (PHP)

请求简单;我需要访问者到我的网站有一定的cookie查看网站。当用户进入my.site.com/anydirectory/anypage.php时,他们需要被重定向到my.site.com/index.php,在那里他们会得到警告并选择走开继续。如果他们继续,将设置一个cookie并将他们带到最初请求的页面(并且最终可以访问整个网站,至少90天)。如果他们选择走开,则设置不同的cookie,并将他们重定向到my.site.com/bummer.php,并将继续定向到 mer.php页面,直到90天结束或他们删除cookie,以先到者为准。

这是我的;

我把这个放在我所有页面的顶部…

<?php if (!isset($_COOKIE['youwerewarned'])) {
header("Location: /index.php?url=http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]")
}
?>

…然后我把它放在我的index.php页面的顶部…

<?php
// change to your domain name.
$domain = "my.site.com";
// Get the requested page, add it to our domain so we can create an enter link
if (isset($_GET['request'])) {
$request ="<a href='http://$domain" . $_GET['request'] . "'>";
}
// The exit link
{
$leave ="<a href='http://my.site.com/bummer.php'>";
}
// Set a cookie so we are not redirected back here again. You can set how long the cookie is good.
if (!isset($_COOKIE['youwerewarned'])) {
setcookie('youwerewarned','true',time()+6048... . $domain);
}
?>

…然后我把它放在index.php页面的适当位置…

<?php echo $leave ?>WALK AWAY</a>&nbsp;&nbsp;&nbsp;<?php echo $request ?>CONTINUE</a>

…我错过了什么?

据我所知,您缺少一个结束/结束分号:

header("Location: /index.php?url=http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]")

:

header("Location: /index.php?url=http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");

然后是这一行,我不知道这=> ... .是不是你实际代码的一部分

setcookie('youwerewarned','true',time()+6048... . $domain);

读作

setcookie("youwerewarned", "true", time()+6048, "/", $domain);

如果错误报告打开,将发出

信号

解析错误:语法错误,意外'}',期望','或';'在…(文件路径)在X行