php - 首次访问者 Cookie

标签 php cookies

对于我的网站,我想使用 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/

相关文章:

android - 使用 .setAcceptThirdPartyCookies 响应 native 应用程序

php - 在 PHP 中检测 cURL 超时

php - 将网站从我的计算机移动到主机或服务器?

PHP 5.5,什么情况下PHP会导致committed memory很高

cookies - 基于 amp-form session 的后端和第 3 方 cookie

java - 读取源代码,将cookies和字段数据传递到站点

jQuery AJAX 响应设置 Cookie header

php - 如何根据多种规范从mysql数据库中选择行

php - LEFT JOIN SQL 语句对性能的影响

javascript - JQuery Cookie 设置过期时间