对于我的网站,我想在页面中间为第一次访问者创建一个链接,上面写着类似于“第一次,单击此处”的PHP。
发布于 2011-04-20 05:44:10
从理论上讲,你可以通过cookies来做到这一点,但除了要求他们注册一个帐户并在他们第一次登录时向他们显示消息之外,没有任何保证的方法来检测“首次访问者”。
原因是cookies可能会从浏览器中清除,人们可能会更换计算机/浏览器,cookies最终会过期,这取决于你的目标是什么,你可能最终会惹恼现有用户,假设他们是新用户。
不管怎样,说的够多了。您的代码可能如下所示:
<?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; ?>干杯!
发布于 2011-04-20 05:39:10
有一个在“点击”时创建cookie的函数,并提示没有cookie的每个人。
Setting a cookie is pretty trivial in PHP。
发布于 2011-04-20 05:39:19
if(isset($_GET['firsttimer'])){
// ok, lets set the cookie
setcookie('firsttimer','something',strtotime('+1 year'),'/');
$_COOKIE['firsttimer']='something'; // cookie is delayed, so we do this fix
}
if(!isset($_COOKIE['firsttimer']){
// if cookie ain't set, show link
echo '<a href="?firsttimer">First time, click here</a>';
}else{
// not firsttimer
echo 'Welcome back!';
}https://stackoverflow.com/questions/5722990
复制相似问题