对于我的网站,我想使用 PHP 在页面中间为首次访问者创建一个链接,内容类似于“首次访问,请单击此处”。
最佳答案
理论上您可以使用 cookie 来做到这一点,但是除了要求他们注册帐户并在他们第一次登录时向他们显示消息之外,没有任何保证可以检测到“首次访问者”的方法。
原因是 cookie 可能会从浏览器中清除,人们可能会更换计算机/浏览器,cookie 最终会过期,并且根据您的目标,您可能最终会惹恼现有用户(假设他们是新用户)。
总之,够了。您的代码可能如下所示:
<?php
// Top of the page, before sending out ANY output to the page.
$user_is_first_timer = !isset( $_COOKIE["FirstTimer"] );
// Set the cookie so that the message doesn't show again
setcookie( "FirstTimer", 1, strtotime( '+1 year' ) );
?>
<H1>hi!</h1><br>
<!-- Put this anywhere on your page. -->
<?php if( $user_is_first_timer ): ?>
Hello there! you're a first time user!.
<?php endif; ?>
干杯!
关于php - 首次访问者 Cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5722990/