首次访问Cookie [英] First Time Visitor Cookie

查看:266
本文介绍了首次访问Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的网站,我想在网页中间创建一个链接,首次访问使用PHP第一次,点击这里。

For my site, I want to create a link in the middle of the page for first time visitors that says something like "First time, click here" using PHP.

推荐答案

你可以在理论上使用cookie,但没有保证的方式来检测第一次访问者,而不是要求他们注册一个帐户,并显示他们的消息第一次他们登录。

You could in theory do it with cookies, but there's no guaranteed way to detect "first time visitors" other than asking them to register for an account and show them the message the first time they log in.

原因是,Cookie可能会从浏览器中清除,人们可能会更改计算机/浏览器,Cookie最终会过期,具体取决于您的目标是什么可能最终令现在的用户假设他们是新的。

The reason is that cookies may get cleaned from the browser, people may change computers / browsers, cookies will eventually expire and depending on what your goal is you may end up annoying existing users assuming they're new.

无论如何,足够了。您的代码可能如下所示:

Anyway, enough of that. Your code could look something like this:

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

干杯!

这篇关于首次访问Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆