Detecting user's screen size and resolution

If you're developing a site, you should carefully consider how your web pages appear on different kinds of screens. Today people access website on devices with different types of screens, you should account for some factors that affect the way your web pages appear on these devices.

Javascript

one simple way is to identify the screen width with the help of javascript and then render the webpage accordingly. Following is sample code to identify screen width and set it in cookie.
<?php

if(isset($_COOKIE["device_resolution"]))
 $screen_res = $_COOKIE["device_resolution"];
else //cookie is not found set it using Javascript
{
?>
 
<?
}

echo "Your screen resolution is set at ". $screen_res;
?>

PHP

With php, it is very easy to identify device using $_SERVER['HTTP_USER_AGENT'].

Usining strpos() function find position of first occurrence of a string.

 
 <?php
$iphone = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
$android = strpos($_SERVER['HTTP_USER_AGENT'],"Android");
$blackberry = strpos($_SERVER['HTTP_USER_AGENT'],"BlackBerry");
$ipod = strpos($_SERVER['HTTP_USER_AGENT'],"iPod");

if ($iphone || $android || $ipod || $blackberry)
{

header('Location: http://m.websitesite.com/');
}

?>

Comments

  1. Thanks especially for the php part, just couldn´t figure out how to ignore, so not just display: none in the css, a part of my code. Thank you!

    ReplyDelete

Post a Comment

Popular posts from this blog

autoload class

MySql Slow Queries