2012-02-20 65 views
0

有沒有辦法檢索智能手機的模式?如果它處於橫向或縱向模式?所有可以在PHP中訪問?用PHP檢索風景/人像模式

看來Session不能在JS中訪問。但你可以用Cookies來做到這一點。下面是一個代碼段(未測試)

function createCookie(name,value,days) { 
    if (days) { 
     var date = new Date(); 
     date.setTime(date.getTime()+(days*24*60*60*1000)); 
     var expires = "; expires="+date.toGMTString(); 
    } 
    else var expires = ""; 
    document.cookie = name+"="+value+expires+"; path=/; domain=.example.com"; 
} 
function eraseCookie(name) { 
createCookie(name,"",-1); 
} 

window.onorientationchange = function() { 
    /*window.orientation returns a value that indicates whether iPhone is in portrait mode, landscape mode with the screen turned to the 
left, or landscape mode with the screen turned to the right. */ 
var orientation = window.orientation; 

switch(orientation) { 

case 0: 
    // If in portrait mode 
    document.getElementById("google_map").style.width="300px"; 
    document.getElementById("google_map").style.height="200px"; 
    eraseCookie("orientation"); 
    createCookie('orientation','portrait','0'); 
    break; 

case 90: 
    // If in landscape mode with the screen turned to the left 
    document.getElementById("google_map").style.width="450px"; 
    document.getElementById("google_map").style.height="325px"; 
    eraseCookie("orientation"); 
    createCookie('orientation','landscape','0'); 
    break; 

case -90: 
    // If in landscape mode with the screen turned to the right 
    document.getElementById("google_map").style.width="450px"; 
    document.getElementById("google_map").style.height="325px"; 
    eraseCookie("orientation"); 
    createCookie('orientation','landscape','0'); 
    break; 

case 180: 
    // If in portrait mode with the screen flipped 
    document.getElementById("google_map").style.width="300px"; 
    document.getElementById("google_map").style.height="200px"; 
    eraseCookie("orientation"); 
    createCookie('orientation','portrait','0'); 
    break; 
    } 
} 

在PHP:

$orientation = $_COOKIE["orientation"]; 
if($orientation=="portrait"){ 
    // do something 
}else if($orientation=="landscape"){ 
    // do something different 
}else{ 
    // fallback 
} 

hereherehereherehere兩者。

+3

PHP是服務器端,所以不能看東西屏幕方向。你需要使用類似Javascript的東西來檢測,然後將信息存儲在cookie/session中,然後重新加載並讀取PHP中的cookie /會話。 – BenOfTheNorth 2012-02-20 16:42:26

+0

@BenGriffiths或使用AJAX – PeeHaa 2012-02-20 16:47:58

+0

的確如此 - 我想這取決於一旦他們知道方向是什麼,將會做什麼。 – BenOfTheNorth 2012-02-20 16:49:31

回答

1

您可以做的最多的是在Javascript中檢測到,然後將該數據發送回服務器。查看window.orientationorientationchange事件。