cloudll Posted February 11, 2016 Share Posted February 11, 2016 (edited) Hey guys, I am using this code to use javascript to send the browser window width to a php session variable. 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. <button onclick="myFunction()">Get Width</button> <script> function myFunction() { alert(window.innerWidth); } </script> I know this isn't just php but I was hoping someone would have any idea as to whats going on? Thanks for any help EDIT: I uploaded a demo: http://www.myprimaryweb.co.uk/checkWidth/ Edited February 11, 2016 by cloudll Quote Link to comment https://forums.phpfreaks.com/topic/300791-phpjs-results-differ-on-mobile-device/ Share on other sites More sharing options...
QuickOldCar Posted February 11, 2016 Share Posted February 11, 2016 Not all devices and browsers work the same. If can do what you need using css and media queries will be better off. Here's something I did a few years back for a client for saving some sizes dealing with video content areas. <?php if(isset($_COOKIE["screen_width"]) && trim($_COOKIE["screen_width"]) !=''){ $screen_width = trim($_COOKIE["screen_width"]); $screen_width = calc_res($screen_width, 66, 2); $screen_height = round($screen_width / 1.78); }else{ ?> <script type="text/javascript"> var winW = ''; if (document.body && document.body.offsetWidth) { winW = document.body.offsetWidth; } if (document.compatMode=='CSS1Compat' && document.documentElement && document.documentElement.offsetWidth ) { winW = document.documentElement.offsetWidth; } if (window.innerWidth) { winW = window.innerWidth; } var winH = ''; if (document.body && document.body.offsetHeight) { winH = document.body.offsetHeight; } if (document.compatMode=='CSS1Compat' && document.documentElement && document.documentElement.offsetHeight ) { winH = document.documentElement.offsetHeight; } if (window.innerHeight) { winH = window.innerHeight; } if(window.innerWidth <= 800 && window.innerHeight <= 600) { winW="640"; winH="480"; } function setCookie(c_name,value,exdays,domain,path){ var exdate=new Date(); exdate.setDate(exdate.getDate() + exdays); var c_value=escape(value) + ((exdays==null) ? "" : ("; expires="+exdate.toUTCString())); cookie=c_name + "=" + c_value; if (domain){ cookie += "domain=" + domain + ";"; } if (path){ cookie += "path=" + path + ";"; } document.cookie=cookie; } setCookie("screen_width",winW,60,"/",".site.com"); setCookie("screen_height",winH,60,"/",".site.com"); </script> <?php $screen_width = "100%"; $screen_height = "600"; }//end set screen size cookie ?> Quote Link to comment https://forums.phpfreaks.com/topic/300791-phpjs-results-differ-on-mobile-device/#findComment-1531032 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.