第一次访客Cookie

Rya*_*yan 4 php cookies

对于我的网站,我想在页面中间为首次访问者创建一个链接,使用PHP说"首次点击此处".

Sui*_*oth 5

理论上你可以使用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; ?>
Run Code Online (Sandbox Code Playgroud)

干杯!