NEED QUICK PHP HELP: How can I fix this code?

Indiigo007

Junior Member
Joined
Nov 11, 2009
Messages
189
Reaction score
191
Hi, I have the following code to detect user agent:

Code:
<?php

function agent($browser) {
$useragent = $_SERVER['HTTP_USER_AGENT'];
return strstr($useragent,$browser);
}


if(agent("Firefox") != FALSE) {
header('Location:http://location1.com');
}
else if(agent("Opera") != FALSE) {
header('Location:http://location2.com');
}
else if(agent("Safari") != FALSE) {
header('Location:http://location3.com');
}
else if(agent("MSIE") != FALSE) {
header('Location:http://location4.com');
}
else {
header('Location:http://location5.com');
}
?>

However, it thinks Chrome is "Safari." How do I modify this (or add to this) so it distinguishes Chrome from Safari?

Any help would be much appreciated.
 
ugh! looking at that dizzies me. lol. sry i just had to comment. i hate php code stuff. hopefully others can help you
 
Code:
<?php

$user_agent = $_SERVER['HTTP_USER_AGENT'];

$firefox_url = 'http://location1.com/';
$opera_url   = 'http://location2.com/';
$safari_url  = 'http://location3.com/';
$msie_url    = 'http://location4.com/';
$chrome_url  = 'http://location5.com/';
$default_url = 'http://location6.com/';

if(preg_match('/firefox/i', $user_agent)) {
	header("Location: $firefox_url", true, 301);
	exit;

} elseif(preg_match('/opera/i', $user_agent)) {
	header("Location: $opera_url", true, 301);
	exit;

} elseif(preg_match('/safari/i', $user_agent)) {
	header("Location: $safari_url", true, 301);
	exit;

} elseif(preg_match('/msie/i', $user_agent)) {
	header("Location: $msie_url", true, 301);
	exit;

} elseif(preg_match('/chrome/i', $user_agent)) {
	header("Location: $chrome_url", true, 301);
	exit;

} else {
	header("Location: $default_url", true, 301);
	exit;

}

?>
 
Chrome is Safari based, this is why you saw it like that..
 
Back
Top