Jump to content

Browser detection (suggestion)


eevan79

Recommended Posts

Here is my simple script for user browser and OS detection:

 

$agent = $_SERVER['HTTP_USER_AGENT'];
  
  if (strstr($agent,"Windows")) {$os = "Windows";}
  if (strstr($agent,"Windows NT 6")) {$os = "Windows 7";}
  if (strstr($agent,"Linux")) {$os = "Linux";}
  if (strstr($agent,"Intel Mac OS X")) {$os = "OS X";}
  if (strstr($agent,"OpenBSD")) {$os = "BSD";}
  if (strstr($agent,"FreeBSD")) {$os = "FreeBSD";}
  
  if (strstr($agent,"Firefox/3")) {$br = "Firefox 3";}
  if (strstr($agent,"Firefox/2")) {$br = "Firefox 2";}
  if (strstr($agent,"Firefox/1")) {$br = "Firefox 1";}
  if (strstr($agent,"Phoenix/")) {$br = "Phoenix (Mozilla lite)";}
  if (strstr($agent,"MSIE 6")) {$br = "IE 6";}
  if (strstr($agent,"MSIE 7")) {$br = "IE 7";}
  if (strstr($agent,"MSIE 8")) {$br = "IE 8";}
  if (strstr($agent,"Netscape6/6")) {$br = "Netscape 6";}
  if (strstr($agent,"Opera")) {$br = "Opera";}
  if (strstr($agent,"Opera") and strstr($agent,"Version/10")) {$br = "Opera 10";}
  if (strstr($agent,"Opera") and strstr($agent,"Version/9")) {$br = "Opera 9";}
  if (strstr($agent,"Chrome") and strstr($agent,"Chrome/5")) {$br = "Chrome 5";}
  if (strstr($agent,"Safari/")) {$br = "Safari";}
  if (strstr($agent,"Safari/") and strstr($agent,"Version/5")) {$br = "Safari 5";}
  if (strstr($agent,"Safari/") and strstr($agent,"Version/4")) {$br = "Safari 4";}
  if (strstr($agent,"Safari/") and strstr($agent,"Version/3")) {$br = "Safari 3";}
  if (strstr($agent,"SeaMonkey/") and strstr($agent,"Gecko")) {$br = "SeaMonkey";}

  echo "Browser: <b>$br</b>, OS: <b>$os</b>";

I found some information from here: http://www.zytrax.com/tech/web/browser_ids.htm

 

Is there any better solution for this, or script above is accurate?

Link to comment
Share on other sites

Thanks, I have searched for some browser caps, and its basicaly user_agent database :) .

 

I dont need so much information, just for most used browsers and OS. Also, I have tested this script above with different browsers and OS and its working fine. :)

 

If anyone have better suggestion, feel free to share with us. :)

Link to comment
Share on other sites

browscap is an ini file full of browsers and information about them, if you want to include a huge chunk of unattractive if statements for user agents than you'd do it your way :)

 

browscap is the best alternative to this.. and probably better than what you've got going because new browscap.ini files come out whenever a new browser comes out.. + you only check for what 10 browsers? most browscap.ini variations have pretty much every distributed browser besides homemade ones from kids with visual basic..

 

ALSO your checks are pretty redundant in the way that if it alrdy matches "windows 7" why would you check for linux.. you feel me?

 

 

Link to comment
Share on other sites

Ok, I understand.

 

Anyway, I just want to make small stats for users and their OS/Browsers. Dont need huge browsers database. For example:

FF - 45%, IE - 15%, Opera - 25%, Netscape - 5%

and same for OS (Win - Xp/7/98, Linux... ) .

 

I can also try this with browcap. Solved and thanks for suggestions.

Link to comment
Share on other sites

  • 3 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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