I need to find the screen resolution of a users screen who visits my website?
You can't do it with pure PHP. You must do it with JavaScript. There are several articles written on how to do this.
Essentially, you can set a cookie or you can even do some Ajax to send the info to a PHP script. If you use jQuery, you can do it something like this:
jquery:
$(function() { $.post('some_script.php', { width: screen.width, height:screen.height }, function(json) { if(json.outcome == 'success') { // do something with the knowledge possibly? } else { alert('Unable to let PHP know what the screen resolution is!'); } },'json'); });
PHP (some_script.php)
<?php // For instance, you can do something like this: if(isset($_POST['width']) && isset($_POST['height'])) { $_SESSION['screen_width'] = $_POST['width']; $_SESSION['screen_height'] = $_POST['height']; echo json_encode(array('outcome'=>'success')); } else { echo json_encode(array('outcome'=>'error','error'=>"Couldn't save dimension info")); } ?>
All that is really basic but it should get you somewhere. Normally screen resolution is not what you really want though. You may be more interested in the size of the actual browser's view port since that is actually where the page is rendered...
Directly with PHP is not possible but...
I write this simple code to save screen resolution on a PHP session to use on an image gallery.
<?php session_start(); if(isset($_SESSION['screen_width']) AND isset($_SESSION['screen_height'])){ echo 'User resolution: ' . $_SESSION['screen_width'] . 'x' . $_SESSION['screen_height']; } else if(isset($_REQUEST['width']) AND isset($_REQUEST['height'])) { $_SESSION['screen_width'] = $_REQUEST['width']; $_SESSION['screen_height'] = $_REQUEST['height']; header('Location: ' . $_SERVER['PHP_SELF']); } else { echo '<script type="text/javascript">window.location = "' . $_SERVER['PHP_SELF'] . '?width="+screen.width+"&height="+screen.height;</script>'; } ?>
New Solution If you need to send another parameter in Get Method (by Guddu Modok)
<?php session_start(); if(isset($_SESSION['screen_width']) AND isset($_SESSION['screen_height'])){ echo 'User resolution: ' . $_SESSION['screen_width'] . 'x' . $_SESSION['screen_height']; print_r($_GET); } else if(isset($_GET['width']) AND isset($_GET['height'])) { $_SESSION['screen_width'] = $_GET['width']; $_SESSION['screen_height'] = $_GET['height']; $x=$_SERVER["REQUEST_URI"]; $parsed = parse_url($x); $query = $parsed['query']; parse_str($query, $params); unset($params['width']); unset($params['height']); $string = http_build_query($params); $domain=$_SERVER['PHP_SELF']."?".$string; header('Location: ' . $domain); } else { $x=$_SERVER["REQUEST_URI"]; $parsed = parse_url($x); $query = $parsed['query']; parse_str($query, $params); unset($params['width']); unset($params['height']); $string = http_build_query($params); $domain=$_SERVER['PHP_SELF']."?".$string; echo '<script type="text/javascript">window.location = "' . $domain . '&width="+screen.width+"&height="+screen.height;</script>'; } ?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With