简体   繁体   English

使用php中的mobiledetect脚本,每24小时仅重定向一次页面

[英]redirect to page only once every 24 hour using mobiledetect script in php

I have the following code : 我有以下代码:

include("Mobile_Detect.php");

$detect = new Mobile_Detect();
if ($detect->isMobile()) {
$parsedUrll = curPageURL();
$wwwtom = str_replace("www", "m", $parsedUrll);
header("location: $wwwtom");
exit;
}

Which redirects website visitors to the mobile website if they are using a mobile device. 如果网站访问者使用移动设备,则会将其重定向到移动网站。 the problem is that the code redirects all the time making it impossible for mobile users to access the computer website. 问题是代码一直重定向,使移动用户无法访问计算机网站。 I want the mobile users to have the option to go back to the normal website if they clicked on a button. 我希望移动用户可以选择在点击按钮时返回普通网站。 but I cannot really do that because of the redirection code that I have now. 但由于我现在拥有的重定向代码,我无法真正做到这一点。 How can I fix the code, so it will only redirect once every 24 hours. 我该如何修复代码,因此它每24小时只会重定向一次。 suggestions, ideas, solution, all are welcome. 建议,想法,解决方案,欢迎所有人。

Use this for the mobile detection: 用于移动检测:

include 'Mobile_Detect.php';

$detect = new Mobile_Detect();
if ($detect->isMobile() && !isset($_COOKIE['use_desktop'])) // check if mobile and does not prefer desktop
{
    $parsedUrll = curPageURL();
    $wwwtom = str_replace('www', 'm', $parsedUrll);
    header("Location: $wwwtom");
    exit;
}

Use link like this for going to desktop, or use a $_GET query: 使用这样的链接转到桌面,或使用$_GET查询:

<a href='desktop.php'>View Desktop Version</a>

In desktop.php use this: desktop.php使用此:

define('COOKIE_LIFETIME_ONE_DAY', $_SERVER['REQUEST_TIME'] + 86400);
setcookie('use_desktop', '1', COOKIE_LIFETIME_ONE_DAY);
header("Location: http://www.mysite.com/"); // direct to desktop site
exit;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM