【发布时间】:2014-08-20 22:31:31
【问题描述】:
简单的请求;我需要我网站的访问者拥有特定的 cookie 才能查看该网站。当用户访问 my.site.com/anydirectory/anypage.php 时,他们需要被重定向到 my.site.com/index.php 向他们发出警告并选择走开或继续。如果他们继续,则会设置一个 cookie,并将他们带到最初请求的页面(并最终可以访问整个站点,至少 90 天)。如果他们选择 Walk Away,则设置不同的 cookie 并将他们重定向到 my.site.com/bummer.php,并将继续被定向到 bummer.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> <?php echo $request ?>CONTINUE</a>
...我错过了什么?
【问题讨论】:
-
这不起作用如何?你检查过cookies是否真的被设置了吗?也许您遇到了过早的标头发送问题。
-
“我错过了什么?” - 标题的分号。开启error reporting,将向
Parse error: syntax error, unexpected '}'in...IF 发出信号,这是您的实际工作代码,而不仅仅是复制/粘贴错误。