php 第一次访问时显示不同的页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8752398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:25:38  来源:igfitidea点击:

Show different page if first time visit

phpcookies

提问by Ben Thomson

I found a snippet of code that redirects if it's the first visit, but when I tried to use it, it just stayed at that code. I don't really understand too much about the cookies and how it works, so maybe you can help! Here's the PHP code:

我发现了一段代码,如果它是第一次访问,它会重定向,但是当我尝试使用它时,它只是停留在该代码中。我对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!

    }

?>

So how could I fix it so if it's your first visit to the site it brings you to a certain page, and if not, the default?

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

回答by Frederick Marcoux

You can use this code:

您可以使用此代码:

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

It will check if you have a cookie named "firsttime" and if not, it will create it and redirect to your FIRSTTIME page... If yes, it will just redirect you to the website...

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

回答by Andy

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

?>

You just make an if statement to check whether there is a session set, if not, you know its there first time. Though, since it is not a cookie, anytime you quit the browser, it will assume it is the first time, even if it is never the first time.

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

回答by Blazer

If sessions/cookies are to difficultly you can save the IP of the visitor. When IP exist show page 1 when IP is new redirect to other page?

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

回答by PeeHaa

For more information see 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
    }

?>

回答by L422Y

  <?php

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

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

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

  ?>

回答by Johnjhs

  <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 or PeeHaa would the above script also work to bring up a window before they enter the site rather than a page.

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