Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the screen resolution using PHP

I need to find the screen resolution of a users screen who visits my website?

like image 398
Elitmiar Avatar asked Oct 01 '09 14:10

Elitmiar


2 Answers

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...

like image 161
KyleFarris Avatar answered Sep 19 '22 17:09

KyleFarris


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>'; } ?> 
like image 21
Carlos Ricardo Schmitz Avatar answered Sep 16 '22 17:09

Carlos Ricardo Schmitz