Jump to content

Mobile device detection


Slip

Recommended Posts

I want one url for a site that can be access by mobile devices as well as desktop pc's. So, my index page will have to contain some kind of script to detect what the user is browsing on and then redirecting to the appropriate directory. Problem is I cannot work out how to detect what the screen is of the user. I've tried Javascript to detect the screen.width and screen.height but when I tested the result in my Sony Ericsson mobile phone the output was 800 x 5000! So clearly this is not fool proof.

 

Is there a way I can do this with PHP? I've read some odd posts referencing the USER_AGENT but it still seems a gray area.

Link to comment
https://forums.phpfreaks.com/topic/127557-mobile-device-detection/
Share on other sites

user agent would be the way to go.

 

The user agent is a param in the header that tells the server what browser software the client has.

 

In this case, you need to find out what the user agent of a PPC, then make a function to switch the content based on the user agent.

 

Chris

 

 

I've not had much experience with the USER_AGENT variable but I am guessing with each device that variable string will have the device manufacturer amongst it? If so, I guess I could loop through an array of manufacturers and once detected in the string then send the user to the mobile section. Does this make sense?

 

$userAgents = array("nokia","SonyEricsson","HTC","LG");

for($i=0; $i<count($userAgents); $i++) {
   $chunk = explode($userAgents[$i], $_SERVER['HTTP_USER_AGENT']);
   if(count($chunk) > 1) {
      header("Location: mobile.html");
      return;
   }
}

header("Location: standard.html");

Okay, I've just done a quick test and the following code looks good. Feel free to use this code as you please...

 


$mobileAgents = array("AvantGo","Windows CE","Blazer","Palm","BlackBerry","SonyEricsson","Smartphone","MMP","Nokia","LG","NetFront","RIM","Xiino","WAP","Samsung");
$userAgent = $_SERVER['HTTP_USER_AGENT'];

for($i=0; $i<count($mobileAgents); $i++) {
$chunk = explode($mobileAgents[$i], $userAgent);
if(count($chunk) > 1) {
	echo "This page is being viewed on a mobile device";
	return;
	}
}

echo "Standard device page";

 

P.S. Can anyone tell me how to colour my code in the posts on this forum?

  • 8 months later...

I have published the last version of "Apache Mobile Filter", this open source project have, in the first six months, more than 600 downloads from sourceforge and I think the same from CPAN.

 

The Apache Mobile Filter allows you to access WURFL from any programming language, not just Java and php that is traditionally used for dynamic mobile web sites.

 

The module detects the mobile device and passes the WURFL capabilities on to the other web application as environment variables. It can also be used to resize images on the fly to adapt to the screen size of the mobile device.

 

Try it and let me know your opinion.

 

For more info: http://www.idelfuschini.it/it/apache-mobile-filter-v2x.html

Test Page: http://apachemobilefilter.nogoogle.it/php_test.php

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.