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

时间:2014-08-20 22:31:32

标签: php redirect cookies

简单的要求;我需要访问我网站的访问者才能查看该网站。当用户访问 my.site.com/anydirectory/anypage.php 时,需要将其重定向到 my.site.com/index.php 会给予警告并选择离开继续。如果他们继续,则设置cookie并将其带到最初请求的页面(最终可访问整个站点,至少90天)。如果他们选择Walk Away,则会设置不同的Cookie并将其重定向到 my.site.com/bummer.php ,并将继续定向到< strong> 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>&nbsp;&nbsp;&nbsp;<?php echo $request ?>CONTINUE</a>

......我错过了什么?

1 个答案:

答案 0 :(得分:0)

我可以告诉你,你错过了一个结束/结束分号:

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

做的:

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

然后有这条线,我不知道这是否=&gt; ... .是您实际代码的一部分。

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

应该读作

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

开启error reporting会发出信号

  

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

缺少分号。