[英]Redirecting mobile-desktop versions manually
I have this code in my homepage 我的主页中有此代码
<?php
if(isset($_SESSION['mobile'])){
if($_SESSION['mobile']==1){
echo '
<script>
if( /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) ) {
window.location = "mobile/index.php";
}
</script>';
}
}
else{
$_SESSION['mobile']=1;
echo '
<script>
if( /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) ) {
window.location = "mobile/index.php";
}
</script>';
}
?>
and on the mobile version of my website, to get back to the destop version I inserted a link to this almost empty "redirect.php" page 在我的网站的移动版本上,要返回到停止版本,我插入了指向此几乎为空的“ redirect.php”页面的链接
<?php
$_SESSION['mobile']=0;
header("location: ../index.php");
exit;
?>
but I am not getting the desired behavior: once I enter the website with my mobile, I am correctly redirected to the mobile version, but once I click on the link to get the desktop version, I loop back into the mobile version. 但是我并没有得到期望的行为:使用手机进入网站后,我正确地重定向到了手机版本,但是一旦单击链接以获取桌面版本,我便回到了手机版本。 What am I missing? 我想念什么?
Thanks! 谢谢! :) :)
Remember to call session_start()
function before using sessions. 记住在使用会话之前先调用session_start()
函数。
Better use this to reduce code: 更好地使用它来减少代码:
<?php session_start();
if (isset($_GET["desktop"])) {
// DESKTOP
$_SESSION["mobile"] = 0;
...
} else {
// MOBILE
if (!isset($_SESSION["mobile"])) { $_SESSION["mobile"] = 1; }
if ($_SESSION["mobile"] == 1){ echo '<script>...</script>'; }
...
}
?>
And link to the homepage with ?desktop=1
to switch to the desktop version. 并使用?desktop=1
链接到主页以切换到桌面版本。
Based on your current code - You should have a separate session var that tell you that the user has manually reqeusted control. 根据您当前的代码-您应该有一个单独的会话变量,该变量告诉您用户已手动请求控件。
<?php
if(!$_SESSION['manuall_overide'])
if(isset($_SESSION['mobile'])){
if($_SESSION['mobile']==1){
echo '
<script>
if( /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) ) {
window.location = "mobile/index.php";
}
</script>';
}
}
else{
$_SESSION['mobile']=1;
echo '
<script>
if( /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) ) {
window.location = "mobile/index.php";
}
</script>';
}
}
?>
<?php
$_SESSION['manuall_overide']=1;
header("location: ../index.php");
exit;
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.