Get complete CodeIgniter blog project for free!

Downloaded more than 161 times, this is full project must-study for serious developers!

By submitting this form, you agree to our Terms of Service and Privacy Policy.

Detect Mobile Device In PHP

n this article, I am going to show how to Detect Mobile Device In PHP and redirect user to mobile site. Mobile devices are increasing day by day and it's pretty difficult to present appropriate version of a website to users. So, some website going to make a dual version of a website for users like Mobile version for mobile device and PC version for a computer device. This article helps you to Detect Mobile Device In PHP. It's very easy to detect mobile devices, follow these instructions step by step.

Step-1: Create a PHP file to Detect Mobile Device


At first, we need to create a PHP file to detect mobile devices. So, we are going to create a PHP file name as detect.php and write down this code bellow.

detect.php

<?php
class Detect
{


public function mobile(){
if(stristr($_SERVER['HTTP_USER_AGENT'],'windows')&&!stristr($_SERVER['HTTP_USER_AGENT'],'windows ce')){
return false;
}
if(eregi('up.browser|up.link|windows ce|iemobile|mini|mmp|symbian|midp|wap|phone|pocket|mobile|pda|psp',$_SERVER['HTTP_USER_AGENT'])){
return true;
}
if(stristr($_SERVER['HTTP_ACCEPT'],'text/vnd.wap.wml')||stristr($_SERVER['HTTP_ACCEPT'],'application/vnd.wap.xhtml+xml')){
return true;
}
if(isset($_SERVER['HTTP_X_WAP_PROFILE'])||isset($_SERVER['HTTP_PROFILE'])||isset($_SERVER['X-OperaMini-Features'])||isset($_SERVER['UA-pixels'])){
return true;
}
$a=array('acs-','alav','alca','amoi','audi','aste','avan','benq','bird','blac','blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno','ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-','maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-','newt','noki','opwv','palm','pana','pant','pdxg','phil','play','pluc','port','prox','qtek','qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar','sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-','tosh','tsm-','upg1','upsi','vk-v','voda','w3c ','wap-','wapa','wapi','wapp','wapr','webc','winw','winw','xda','xda-');
if(isset($a[substr($_SERVER['HTTP_USER_AGENT'],0,4)])){
return true;
}
}

}
?>
our detector is now ready. we have created a class name as Detect and this class will detect user device. so now we are going to see how we can use this and redirect a user to a mobile website.

Step-2: Write code for redirect a user


Before we created device detector and now we are ready to use it and redirect a user into a mobile site. Create index.php and write down this code bellow.
index.php 

<?php
require_once('detect.php');

$detect = new Detect;
$mobile = $detect->mobile();

if(!$mobile)
{
header('location: http://m.example.com');
}
else
{
header('location: http://example.com');
}
?>
This is our code and this is help us to redirect users in a mobile website. You can set this code in website header to redirect website mobile device. As an example create header files and write down this code.
header.mobile.php 

 <?php
require_once('detect.php');

$detect = new Detect;
$mobile = $detect->mobile();
if(!$mobile)
{
header("Location: http://example.com");
}
exit;
?>
this is for mobile vesion of a website and another one for PC version.
header.pc.php 

<?php
require_once('detect.php');

$detect = new Detect;
$mobile = $detect->mobile();
if($mobile)
{
header("Location: http://m.example.com");
}
exit;
?>
this is the desktop version of a website you can add these headers in your website into two different versions. When a user browses desktop version of a website with a mobile device then this script will automatically redirect this into mobile version and in the mobile header when a user browses a mobile website with PC then this script will automatically redirect this user to the desktop version of this website.

That's all, I hope you understand this and you can use this. You are free to use this code and you can edit this for further use. If you have any question or comment about this Detect Mobile Device In PHP please don't hesitate to do that, please comment your question in comment section.






Search on PHPAns
Subscribe to PHPAns
Join with our 32000+ subscribers and get our latest articles in your email inbox for free.
Delivered by FeedBurner
We always respect your privacy and take protecting it seriously.
Connect Social With PHPAns
Top