我发现了一段代码重定向,如果它是第一次访问,但是当我尝试使用它时,它只是留在那个代码.我对饼干及其工作原理并不太了解,所以也许你可以提供帮助!这是PHP代码:
<?php
session_start();
if (isset($_SESSION['FirstVisit'])) {
$_SESSION['FirstVisit'] = 1;
header("Location: http://example.com/index.php");
// Don't forget to add http colon slash slash www dot before!
}
?>
Run Code Online (Sandbox Code Playgroud)
那么我怎么能修复它,所以如果你是第一次访问该网站它会带你到某个页面,如果不是,默认情况下?
Fre*_*oux 19
您可以使用此代码:
<?php
if (!isset($_COOKIE['firsttime']))
{
setcookie("firsttime", "no", /* EXPIRE */);
header('Location: first-time.php');
exit();
}
else
{
header('Location: site.php');
exit();
}
?>
Run Code Online (Sandbox Code Playgroud)
它将检查你是否有一个名为"firsttime"的cookie,如果没有,它将创建它并重定向到你的FIRSTTIME页面...如果是,它只会将你重定向到网站......
<?php
session_start();
if (!isset($_SESSION['FirstVisit'])) {
//show site for the first time part
$_SESSION['FirstVisit'] = 1;
header("Location: http://example.com/index.php");
// Don't forget to add http colon slash slash www dot before!
} else { Show normal site }
?>
Run Code Online (Sandbox Code Playgroud)
您只需编写一个 if 语句来检查是否存在会话集,如果没有,您第一次就知道它存在。不过,由于它不是 cookie,因此每当您退出浏览器时,它都会假设这是第一次,即使它从来都不是第一次。
| 归档时间: |
|
| 查看次数: |
28119 次 |
| 最近记录: |