【问题标题】:Show different page if first time visit如果第一次访问显示不同的页面
【发布时间】:2021-11-30 23:59:05
【问题描述】:

我发现了一个 sn-p 代码,如果这是第一次访问,它会重定向,但是当我尝试使用它时,它只是停留在那个代码上。我对cookies及其工作原理不太了解,所以也许你可以帮忙!这是 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!

    }

?>

那么我该如何解决它,如果这是您第一次访问该网站,它会将您带到某个页面,如果不是,则为默认页面?

【问题讨论】:

  • 你没有设置任何cookie,你设置了一个session(它设置了一个cookie,但不应该是持久的)。
  • // Don't forget to add http colon slash slash www dot before! 为什么?相对路径 ./index.php 可以在位置标头中使用

标签: php cookies


【解决方案1】:

您可以使用此代码:

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

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

编辑 2021 请不要使用这种旧方法,而是使用框架或更好的代码。现在已经 10 岁了。

【讨论】:

  • 问题,我以为header函数只能在其他事情发生之前使用?在上述情况下,如果将退出功能重新定位到该链接,为什么之后会有退出功能?如果它实际重新定位,它不应该运行退出函数。抱歉,对标头了解不多。
  • @Andy 我没有在我的代码中使用 exit() 函数,但是是的,您必须将任何 header() 放在任何其他代码之前,因为它会使 标记出现问题...
  • 哦,这意味着:PHP header() 在 标签之前,并且在 标签之前。
  • 这有 13 个赞成票,并被标记为正确答案......但它对我不起作用。如果不是第一次加载,当我尝试发送回 index.php 时出现错误:页面未正确重定向。 Firefox 检测到服务器正在以永远不会完成的方式重定向对该地址的请求。
  • 这个解决方案非常适合我的需要。感谢发帖!
【解决方案2】:
<?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 }

?>

你只需做一个 if 语句来检查是否有一个会话集,如果没有,你第一次知道它在那里。不过,由于它不是 cookie,所以无论何时您退出浏览器,它都会假定这是第一次,即使它从来都不是第一次。

【讨论】:

    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      欲了解更多信息,请参阅the docs

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

      【讨论】:

        【解决方案5】:
          <?php
        
            @session_start();
            $url = 'http://blah.com/default/';
        
            if (!isset($_COOKIE['Visited'])) {
                $_COOKIE['Visited'] = 1;
                $url = 'http://blah.com/firstvisit/';
            }
        
            header("Location: {$url}");
        
          ?>
        

        【讨论】:

        • 请不要隐藏警告。每次你这样做,一只小猫就会死去。
        • 这根本不是建设性的,它被抑制的唯一原因是因为我们不知道这段代码的去向,如果插入到错误的位置,它可能会引发关于已发送标头的警告。抱歉,你的小猫死了。
        【解决方案6】:
          <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>
        <?
        }
        
        ?>
        

        @Frederick 或 PeeHaa 上面的脚本是否也可以在他们进入站点而不是页面之前打开一个窗口。

        【讨论】:

        • 欢迎来到stackoverflow。您发布了一个“答案”,但听起来您实际上有一个问题(?)。如果是这样,您应该改为open a new thread
        猜你喜欢
        • 2013-09-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-23
        • 1970-01-01
        • 2014-03-10
        相关资源
        最近更新 更多