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: ".$width." Height: ".$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?
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: ".$width." Height: ".$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?
Share Improve this question asked Apr 23, 2014 at 8:45 bd_personbd_person 331 gold badge1 silver badge6 bronze badges 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. – Realitätsverlust 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? – No Results Found 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
Reset to default 4You 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.
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.