[英]Redirecting mobile-desktop versions manually
我的主頁中有此代碼
<?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>';
}
?>
在我的網站的移動版本上,要返回到停止版本,我插入了指向此幾乎為空的“ redirect.php”頁面的鏈接
<?php
$_SESSION['mobile']=0;
header("location: ../index.php");
exit;
?>
但是我並沒有得到期望的行為:使用手機進入網站后,我正確地重定向到了手機版本,但是一旦單擊鏈接以獲取桌面版本,我便回到了手機版本。 我想念什么?
謝謝! :)
記住在使用會話之前先調用session_start()
函數。
更好地使用它來減少代碼:
<?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>'; }
...
}
?>
並使用?desktop=1
鏈接到主頁以切換到桌面版本。
根據您當前的代碼-您應該有一個單獨的會話變量,該變量告訴您用戶已手動請求控件。
<?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.