php/js results differ on mobile device

Associate
Joined
11 Oct 2008
Posts
268
Hey guys,

I am using this code to use javascript to send the browser window width to a php session variable.

PHP:
session_start();

$parentDir = basename(dirname($_SERVER['PHP_SELF']));;

if (!isset($_SESSION['screenWidth'])) {
    echo '<script>window.location = "' . "/" . $parentDir . "/" . '?width="+window.innerWidth</script>';
    if (isset($_REQUEST['width'])) {
        $_SESSION['screenWidth'] = $_REQUEST['width'];
        header("Location: " . "http://" . $_SERVER['HTTP_HOST'] . "/" . $parentDir);
    }
}

it works fine on my pc, however, when I try it on my phone it returns a width value of 900, which is wrong.
If i add the javascript directly it returns a true value of 350.

Code:
<button onclick="myFunction()">Get Width</button>
<script>
function myFunction() {
    alert(window.innerWidth);
}
</script>

I uploaded a demo: http://www.myprimaryweb.co.uk/checkWidth/

Does anyone have any ideas whats going on? Cheers
 
Associate
OP
Joined
11 Oct 2008
Posts
268
Thanks, thats helped a lot. The width is perfect. Its not included this example, but the height is a little out. I think it must be the screen.height taking the toolbars into consideration, so, i'm guessing theres nothing I can do about that :p
 
Back
Top Bottom