jcadima
10/21/2015 - 4:28 PM

Getting the screen resolution using PHP

Getting the screen resolution using PHP

<?php
// REF: http://stackoverflow.com/questions/1504459/getting-the-screen-resolution-using-php

/*   SOLUTION 1
This will output a smaller sized headline if the screen is 480px or less. 
So no need to pass any vars using JS or similar.
*/
    echo '<h2 media="screen and (max-width: 480px)">'; 
    echo 'My headline';
    echo '</h2>'; 

    echo '<h1 media="screen and (min-width: 481px)">'; 
    echo 'My headline';
    echo '</h1>'; 



// SOLUTION 2
// save screen resolution on a PHP session
session_start();
if(isset($_SESSION['screen_width']) AND isset($_SESSION['screen_height'])){
    echo 'User resolution: ' . $_SESSION['screen_width'] . 'x' . $_SESSION['screen_height'];
} else if(isset($_REQUEST['width']) AND isset($_REQUEST['height'])) {
    $_SESSION['screen_width'] = $_REQUEST['width'];
    $_SESSION['screen_height'] = $_REQUEST['height'];
    header('Location: ' . $_SERVER['PHP_SELF']);
} else {
    echo '<script type="text/javascript">window.location = "' . $_SERVER['PHP_SELF'] . '?width="+screen.width+"&height="+screen.height;</script>';
}