2012-02-20 3 views
0

スマートフォンのモードを取得する方法はありますか?風景モードまたはポートレートモードの場合は? PHPでアクセス可能なものはどれですか?PHPで風景/ポートレートモードを取得

セッションがJS内でアクセスできないようです。しかし、あなたはクッキーでそれを行うことができます。

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

:ここでは、スニペット(テストしていません)PHPで

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; 
    } 
} 

です。

+3

PHPはサーバー側ですので、画面の向きはわかりません。これを検出するにはJavascriptのようなものを使用し、Cookie /セッションに情報を格納してから、PHPでCookie /セッションを読み込んで読み込む必要があります。 – BenOfTheNorth

+0

@BenGriffithsまたはAJAXを使用する – PeeHaa

+0

確かにはい - 私は方向が何であるかを知ったら何が行われるかによって異なります。 – BenOfTheNorth

答えて

1

ほとんどの場合、Javascriptで検出し、そのデータをサーバーに戻すことができます。 window.orientationorientationchangeイベントをチェックしてください。

関連する問題