[英]How to redirect a “Desktop Desktop”?
My website have two versions. 我的网站有两个版本。 Desktop and Mobile version.
桌面和移动版本。
When the user access my website via smartphone, I point it to "Mobile version" -> "m.mywebsite.com". 当用户通过智能手机访问我的网站时,我将其指向“移动版” - >“m.mywebsite.com”。
To make it, I'm using the project called Mobile Detect 为了实现它,我正在使用名为Mobile Detect的项目
So far so good. 到现在为止还挺好。
In "Mobile version" there is a button ("Switch to Desktop Version") that redirect to "Desktop version". 在“移动版”中,有一个按钮(“切换到桌面版”),重定向到“桌面版”。 The problem is that in "Mobile version", when I clicked in button "Switch to Desktop Version" it get in loop.
问题是在“移动版”中,当我点击“切换到桌面版”按钮时,它会进入循环状态。
For example: 例如:
---> Button "Switch to Desktop Version" is clicked; --->点击按钮“切换到桌面版”;
---> The page is point to "www.mywebsite.com"; --->页面指向“www.mywebsite.com”;
---> The page finishes refresh; --->页面完成刷新;
---> Since the user is on the smartphone, the script is loaded again and the user is redirected to "m.mywebsite.com"; --->由于用户在智能手机上,脚本再次加载,用户被重定向到“m.mywebsite.com”;
How I can solve this? 我怎么能解决这个问题?
How I can redirect the user to Desktop version in smartphone? 如何在智能手机中将用户重定向到桌面版?
My code: 我的代码:
<?php
require_once 'Mobile_Detect.php';
$detect = new Mobile_Detect;
?>
<!DOCTYPE html>
<html>
<head>
<?php if( $detect->isMobile() ) : ?>
<script type="text/javascript">
window.location.href = "http://m.mywebsite.com.br";
</script>
<?php endif ?>
</head>
<body>
<div class="wrapper" style="height: 100vh; display: flex; justify-content: center; align-items: center; flex-direction: column">
<h1>MOBILE VERSION</h1>
<a href="http://www.mywebsite.com.br">SWITCH TO DESKTOP VERSION</a>
</div>
</body>
</html>
--------- UPDATE --------- ---------更新---------
Thank's @Uriel, you could solve the problem. 谢谢@Uriel,你可以解决问题。 Now the page it's working like I want.
现在它的页面正如我想要的那样工作。
<?php
require_once 'Mobile_Detect.php';
$detect = new Mobile_Detect;
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf8">
<?php if( $detect->isMobile() && (!isset($_GET['force_desktop']) || $_GET['force_desktop'] == 'false')) : ?>
<?php if( $detect->isMobile() ) : ?>
<script type="text/javascript">
window.location.href = "http://m.mywebsite.com.br";
</script>
<?php endif ?>
<?php endif; ?>
</head>
<body style="padding: 0; margin: 0">
<div class="wrapper" style="height: 100vh; display: flex; justify-content: center; align-items: center; flex-direction: column">
<h1>DESKTOP VERSION</h1>
</div>
</body>
</html>
Set the address of the link 设置链接的地址
<a href="http://www.mywebsite.com.br">SWITCH TO DESKTOP VERSION</a>
To the address http://www.mywebsite.com.br&force_desktop=true
. 到地址
http://www.mywebsite.com.br&force_desktop=true
。
Then, when checking for mobile, check also that this GET
variable is false
(or doesn't exist): 然后,在检查移动设备时,还要检查此
GET
变量是否为false
(或不存在):
if( $detect->isMobile() && (!isset($_GET['force_desktop']) || $_GET['force_desktop'] == 'false'))
That way, the mobile will load only if there is no true force_desktop
variable, and the user uses mobile. 这样, 只有在没有真正的
force_desktop
变量且用户使用移动设备时,移动设备才会加载。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.