简体   繁体   中英

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. 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:

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

In desktop.php use this:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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