title: Retrieve landscape/portrait mode with PHP
body: <p>Is there a way to retrieve the mode of a smartphone? If it is in landscape or portrait mode? And all that accessible within PHP?</p>
  
  <p>It seems Session cannot be accessed within JS. But you can do that with Cookies. Here is a snippet (not tested)</p>
  
  <pre><code>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;
    }
  }
  </code></pre>
  
  <p>In PHP:</p>
  
  <pre><code> $orientation = $_COOKIE["orientation"];
   if($orientation=="portrait"){
       // do something
  }else if($orientation=="landscape"){
      // do something different
  }else{
    // fallback
   }
  </code></pre>
  
  <p>Taken from <a href="http://www.php-einfach.de/tuts_php_cookies.php" rel="nofollow noreferrer">here</a> and <a href="https://stackoverflow.com/questions/5045053/set-cookie-wih-js-read-with-php-problem">here</a> and <a href="http://www.quirksmode.org/js/cookies.html" rel="nofollow noreferrer">here</a> and <a href="http://ajaxian.com/archives/iphone-windowonorientationchange-code" rel="nofollow noreferrer">here</a> and <a href="http://www.engageinteractive.co.uk/blog/2008/06/19/tutorial-building-a-website-for-the-iphone/" rel="nofollow noreferrer">here</a>.</p>