Jump to content

Recommended Posts

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 by cloudll
Link to comment
https://forums.phpfreaks.com/topic/300791-phpjs-results-differ-on-mobile-device/
Share on other sites

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
?>
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.