Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

is UserAgent PHP Detection correct?

6:03 am on Jan 28, 2011 (gmt 0)

New User

5+ Year Member

joined:Sept 24, 2010
posts: 4
votes: 0

Hello, this is my very first post :-D
I'm a web developer from Japan.
Recently I start to creating a website for smartphone.
I think this User Agent detection (PHP) is correct.
Are they correct? Please help me out.



# smartphone
(ereg("iPhone", $ua))
|| (ereg("Android", $ua))
|| (ereg("Windows Phone", $ua))
|| (ereg("BlackBerry", $ua))
$flg = 's';
# mobile
(ereg("DoCoMo", $ua))
|| (ereg("SoftBank", $ua))
|| (ereg("Vodafone", $ua))
|| (ereg("J-PHONE", $ua))
|| (ereg("UP.Browser", $ua))
|| (ereg("KDDI", $ua))
|| (ereg("WILLCOM", $ua))
|| (ereg("DDIPOCKET", $ua))
|| (ereg("PDXGW", $ua))
|| (ereg("Googlebot-Mobile", $ua))
|| (ereg("Y!J", $ua))
|| (ereg("LD_mobile_bot", $ua))
|| (ereg("moba-crawler", $ua))
|| (ereg("RFCrawler-Mobile", $ua))
|| (ereg("froute.jp", $ua))
|| (ereg("ichiro", $ua))
$flg = 'm';
# PC
$flg = 'p';


Thank you for your interest :-D
8:18 am on Jan 28, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Feb 22, 2009
votes: 0

Hi there m14ktm,

Welcome to the forum!

Only one thing to point out with your code: ereg() [uk3.php.net] is now deprecated, so make sure that where you run this code is running the older version (which is fewer now), OR convert to preg_match() [uk3.php.net] so that you can have backwards and forwards compatibility where ever you run your script.

You code will then only need a minor tweek in the pattern area, so something like this:-

ereg("iPhone", $ua);

would (I think) turn into this:-

preg_match("/^iPhone$/i", $ua);<- i just means case insensitive but can be omitted..

Though I am sure as I will be corrected there :)