如果一个页面被访问超过5次,将访问者重定向到另一个URL? [英] Redirect visitor to a different url if a page is accessed more than 5 times?
问题描述
如果用户访问某个页面超过5次,我将尝试对其进行重定向。
因此,基本思路是,如果用户未登录我的网站,并且正在浏览用户个人资料(profile.php),则此计数将计算cookie会话的点击次数,并重定向到页面上说
I'm trying to redirect a user if they access a page more than 5 times. So the basic idea is if a user is not logged in on my site and they are browsing a users profile (profile.php) then this counts the number of hits that cookie session has had and redirects to a page to say sign up or something.
我是php的新手,不知道从哪里开始。
I'm new to php and wouldn't know where to start. Could someone please show me.
推荐答案
这在PHP中非常容易实现。只需设置一个具有计数值的会话并在每次访问后读取它即可,如果计数为5或更大,则可以重定向。下面是示例代码
This is very easy to implement in PHP. Just set a session with the count value and read it after each access.Then you can redirect if the count is 5 or more. Below is a sample code
!session_id() ? session_start() : null;
if(!isset($_SESSION['page_access_count'])){
$_SESSION['page_access_count'] = 1;
}elseif($_SESSION['page_access_count'] >= 5){
// redirect to signup page
header('Location:/signup.php');
exit;
}
// increase the page access session value
$_SESSION['page_access_count']++;
...
这篇关于如果一个页面被访问超过5次,将访问者重定向到另一个URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!