Php 首次访问时显示不同页面

Php 首次访问时显示不同页面,php,cookies,Php,Cookies,我发现了一段代码,如果它是第一次访问,它会重定向,但当我尝试使用它时,它只是停留在那段代码上。我真的不太了解饼干以及它是如何工作的,所以也许你可以帮我!以下是PHP代码: <?php session_start(); if (isset($_SESSION['FirstVisit'])) { $_SESSION['FirstVisit'] = 1; header("Location: http://example.com/index.php"

我发现了一段代码,如果它是第一次访问,它会重定向,但当我尝试使用它时,它只是停留在那段代码上。我真的不太了解饼干以及它是如何工作的,所以也许你可以帮我!以下是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!

    }

?>
您可以使用以下代码:

<?php
if (!isset($_COOKIE['firsttime']))
{
    setcookie("firsttime", "no", /* EXPIRE */);
    header('Location: first-time.php');
    exit();
}
else
{
    header('Location: site.php');
    exit();
}
?>


它会检查你是否有一个名为“firsttime”的cookie,如果没有,它会创建它并重定向到你的firsttime页面。。。如果是,它只会将您重定向到网站…

如果会话/cookie非常困难,您可以保存访问者的IP。当IP存在时显示第1页当IP是新的重定向到其他页面

有关更多信息,请参阅




$(函数(){
$(“#dialog”).dialog();
});
正文


@Frederick或PeeHaa也会在他们进入网站之前打开一个窗口,而不是一个页面。

你没有设置任何
cookie
,你设置了
会话(设置了cookie,但不应该是持久的)。
//之前别忘了添加http冒号斜杠www.dot为什么?相对路径
/index.php
可在位置标题中使用请不要抑制警告。每次你这样做,一只小猫就死了。这根本不是建设性的,它被抑制的唯一原因是因为我们不知道这段代码要去哪里,如果插入到错误的位置,它可能会发出关于已经发送的头的警告。对不起,你的小猫死了。问题,我以为头函数只能在其他事情发生之前使用?在上述情况下,如果要将退出功能重新定位到该链接,为什么要在之后使用退出功能?如果它实际重新定位,则不应运行退出功能。很抱歉,我不太了解头。@Andy我的代码中没有使用exit()函数,但是是的,您必须将任何头()放在任何其他代码之前,因为这会使标记出现问题…哦,这意味着:PHP头()在标记之前,也在标记之前。哦,我明白您的意思,只要它在任何输出到窗口之前,这是有效的。谢谢。这有13张赞成票,被标记为正确答案。。。但这对我不起作用。如果index.php不是第一次加载,那么当我尝试发送回index.php时会出现一个错误:页面没有正确重定向。Firefox检测到服务器正在以一种永远不会完成的方式重定向对此地址的请求。欢迎使用stackoverflow。你发布了一个“答案”,但听起来你确实有一个问题(?)。如果是这样,你应该改为。
<?php

    if (!isset($_COOKIE['visited'])) { // no cookie, so probably the first time here
        setcookie ('visited', 'yes', time() + 3600); // set visited cookie

        header("Location: http://example.com/index.php");
        exit(); // always use exit after redirect to prevent further loading of the page
    }

?>
  <?php

    @session_start();
    $url = 'http://blah.com/default/';

    if (!isset($_COOKIE['Visited'])) {
        $_COOKIE['Visited'] = 1;
        $url = 'http://blah.com/firstvisit/';
    }

    header("Location: {$url}");

  ?>
<?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 }

?>
  <link rel="stylesheet" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-
ui.css" />
<script src="http://code.jquery.com/jquery-1.8.3.js"></script>
<script src="/resources/demos/external/jquery.bgiframe-2.1.2.js"></script>
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<script>
$(function() {
$( "#dialog" ).dialog();
});
</script> 

<?php
if (!isset($_COOKIE['firsttime']))
{
setcookie("firsttime", "no", /* EXPIRE */);
header('Location: first-time.php');
exit();
}
else
{
?>
<div id="dialog" title="Basic dialog">
<p>text</p>
</div>
<?
}

?>