0

Is there a way to get the screen size through php? I have had a look on google, and found a way of doing it through javascript as follows:

PHP :

 <?php
   $width = "<script>sw=screen.width;</script>"; 
   $height = "<script>sh=screen.height;</script>"; 
   echo "Width&#58; ".$width." Height&#58; ".$height;
 ?>

But this doesn't store anything in $width or $height. is this a good way of going about doing it? or is there another way?

5
  • Ofc not, because PHP gets evaluated before JS. You are storing a string which will be evaluated as a script, but the value of the variable stays unaffected because PHP doesnt even know that theres a script. I dont really see a reason to store the width of the browser windows in PHP, but you will need AJAX for that. Commented Apr 23, 2014 at 8:49
  • PHP is server-side and JS client-side. You can't use JS "inside" of PHP.
    – Tzar
    Commented Apr 23, 2014 at 8:49
  • 1
    possible duplicate of Get window size with php
    – avetisk
    Commented Apr 23, 2014 at 8:54
  • Most important question is WHY do you want to do that?
    – Mog
    Commented Apr 23, 2014 at 8:57
  • Actually I am working on a running project which is done by other person. In that project I need browser width in php. But from google I know that it is not possible in php. Thats why I post this question @Wesley Murch
    – bd_person
    Commented Apr 23, 2014 at 9:03

2 Answers 2

4

You can't do that this way, here is a way to do it :

<?php
session_start();
if(isset($_POST['width'])){
   $_SESSION['screen_size'] = array();
   $_SESSION['screen_size']['width'] = intval($_POST['width']);
   $_SESSION['screen_size']['height'] = intval($_POST['height']);
}


if(!isset($_SESSION['screen_size'])){
?>
<html>
<head>
<script>
function getSize(){
document.getElementById('inp_width').value=screen.width;
document.getElementById('inp_height').value=screen.height;
document.getElementById('form_size').submit();
}
</script>
</head>
<body onload='getSize()'>
<form method='post' id='form_size'>
<input type='hidden' name='width' id='inp_width'/>
<input type='hidden' name='height' id='inp_height'/>
</form>
</body>
</html>


<?php
}else{
    var_dump($_SESSION['screen_size']);
}

This is a simple way, and the page will reload the first time.

You may want to use AJAX.

Also, if someone refuses sessions cookies, this would loop forever, better test if the browser accepts cookies.

2
  • If you like the answer, please validate it, it rewards me for my help :)
    – Loïc
    Commented Apr 23, 2014 at 9:15
  • 1
    I wanted the size available in the browser window, and found that the above code gives me that if I just change value=screen.width; to value=window.InnerWidth; and value=screen.height; to value=window.innerHeight;
    – Roy Grubb
    Commented Jun 1, 2021 at 16:35
1

You cannot get browser width by only using php. PHP is server code.

Try to look at this. You can use ajax to send (post) browser width in your php code.

Hope it helps.

Not the answer you're looking for? Browse other questions tagged or ask your own question.