Jump to content


PHP browser detect.


6 replies to this topic

#1 Power

    Young Padawan

  • Validating
  • Pip
  • 227 posts
  • Gender:Male

Posted 24 November 2005 - 01:04 AM

Hey PHP masters,

I just started on php not too long ago. I was wondering is there a way to detect which browser the user is using with PHP?

Thanks.

#2 Chaos King

    Senior Programmer

  • P2L Staff
  • PipPipPip
  • 676 posts
  • Gender:Male
  • Location:Florida

Posted 24 November 2005 - 01:38 AM

Simply, by using

<?php 

$browser =  $_SERVER["HTTP_USER_AGENT"]; 

echo 'Your Browser is: '. $browser;

?>

Edited by Chaos King, 24 November 2005 - 01:47 PM.


#3 HaloprO

    Requires Armed Escort

  • Members
  • PipPip
  • 310 posts
  • Gender:Male
  • Location:California, USA

Posted 24 November 2005 - 02:52 AM

Hey Chaos King, look a little closer at your code, you'll notice that we don't usually name a variable that shows the browser "ip" :D
And we also don't say, Your ip is firefox, yip

Edited by HaloprO, 24 November 2005 - 02:52 AM.


#4 Power

    Young Padawan

  • Validating
  • Pip
  • 227 posts
  • Gender:Male

Posted 24 November 2005 - 02:52 AM

:D Thank you, thank you, thank you.

#5 Chaos King

    Senior Programmer

  • P2L Staff
  • PipPipPip
  • 676 posts
  • Gender:Male
  • Location:Florida

Posted 24 November 2005 - 01:47 PM

hehehe...

My bad, was reading two topics at the same time. Kinda got both the requests mixed up. ^_^

My apologies.

#6 Power

    Young Padawan

  • Validating
  • Pip
  • 227 posts
  • Gender:Male

Posted 25 November 2005 - 02:25 AM

Well... still helped me a lil bit.

#7 Neil

    Past Staff Member

  • Members
  • Pip
  • 149 posts
  • Gender:Male
  • Location:Australia

Posted 29 November 2005 - 07:30 AM

Umm i found this on php.net the other day and it works a treat
$browser = array (
   "MSIE",			// parent
   "OPERA",
   "MOZILLA",		// parent
   "NETSCAPE",
   "FIREFOX",
   "SAFARI"
);

$info[browser] = "OTHER";

foreach ($browser as $parent) {
   $s = strpos(strtoupper($_SERVER['HTTP_USER_AGENT']), $parent);
   $f = $s + strlen($parent);
   $version = substr($_SERVER['HTTP_USER_AGENT'], $f, 5);
   $version = preg_replace('/[^0-9,.]/','',$version);
  
   if (strpos(strtoupper($_SERVER['HTTP_USER_AGENT']), $parent)) {
   $info[browser] = $parent;
   $info[version] = $version;
   }
}
if ($info[browser] == "MOZILLA") {
$info[browser] = "FIREFOX";
}

echo $info[browser];





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users