对于我的网站,我想在页面中间为首次访问者创建一个链接,使用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; ?>
干杯!