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?

bd_person
  • 23
  • 1
  • 1
  • 6
  • 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. – Realitätsverlust Apr 23 '14 at 08:49
  • PHP is server-side and JS client-side. You can't use JS "inside" of PHP. – Tzar Apr 23 '14 at 08:49
  • 1
    possible duplicate of [Get window size with php](http://stackoverflow.com/questions/19291236/get-window-size-with-php) – avetisk Apr 23 '14 at 08:54
  • Most important question is WHY do you want to do that? – Wesley Murch Apr 23 '14 at 08: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 Apr 23 '14 at 09:03

2 Answers2

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.

Loïc
  • 11,804
  • 1
  • 31
  • 49
  • If you like the answer, please validate it, it rewards me for my help :) – Loïc Apr 23 '14 at 09: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 Jun 01 '21 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.

Community
  • 1
  • 1