/    Sign up×
Community /Pin to ProfileBookmark

Checking browser and carry out action.

How can I check the browser a client is using in PHP and then carry out a different action based upon that?

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@mayooresanApr 09.2008 — <html>

<body>

<script type="text/javascript">

function ajaxFunction()

{

var xmlHttp;

try

{

// Firefox, Opera 8.0+, Safari

xmlHttp=new XMLHttpRequest();

}

catch (e)

{

// Internet Explorer

try

{

xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");

}

catch (e)

{

try

{

xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");

}

catch (e)

{

alert("Your browser does not support AJAX!");

return false;

}

}

}

xmlHttp.onreadystatechange=function()

{

if(xmlHttp.readyState==4)

{

document.myForm.time.value=xmlHttp.responseText;

}

}

xmlHttp.open("GET","time.asp",true);

xmlHttp.send(null);

}

</script>

<form name="myForm">

Name: <input type="text"

onkeyup="ajaxFunction();" name="username" />

Time: <input type="text" name="time" />

</form>

</body>

</html>[/QUOTE]


I got it from w3schools, I hope it may help you!
Copy linkTweet thisAlerts:
@NogDogApr 09.2008 — If looking for a purely PHP, server-side solution, you can use something like [url=http://www.php.net/get_browser]get_browser[/url](), but all such server-side solutions are dependent upon what information is sent by the browser, and as such can never be depended upon to be accurate or consistent (or even to have been sent at all by the browser).
Copy linkTweet thisAlerts:
@HuevoosApr 09.2008 — For server side detection I made a couple of functions, please note that as NogDog said you can't trust 100% on the info the browser sends. I use them for statistic purposes only

here are the functions:

[code=php] function parse_useragent($agent){
$parts = preg_split('/(?(?![^(]*))s(?!()|x{7})/', $agent);
$data = array();
foreach($parts as $key => $value){
switch($key){
case 0:
list($data['product'], $comment) = explode(' (', $value);

$info = analyze_product_comment($comment);
break;
case 1:
$data['engine'] = $value;
break;
case 2:
if(stristr($value, 'version'))
$data['engine'] .= " $value";
else
$data['browser'] = $value;
break;
case 3:
$data['browser'] = $value;
break;
case 4:
$data['browser'] = $value . " " .$parts[3];
break;
}
}
return $data+$info;
}
function analyze_product_comment($value){
$value = str_ireplace(array("(", ")"), "", $value);
$parts = explode("; ", $value);
foreach($parts as $key => $val){
switch($key){
case 0:
if(strtolower($val) == 'compatible'){//Usually IE or bots
$info = array(
'browser' => $parts[1],//Browser
'os' => $parts[2]//OS
);
return $info;
}
$info['platform'] = $val;//Platform
break;
case 1:
if(eregi('[uin]', $val))
$info['security'] = $val;//Security
break;
case 2:
$info['os'] = $val;//OS
break;

case 3:
if(eregi('[a-z-]{2,6}', $val))
$info['language'] = $val;//Language
break;
}
}
return $info;
}[/code]

(more info http://blog.work4bandwidth.com/2/Parsing+the+User+Agent+String+in+PHP/)
×

Success!

Help @sanchez_1960 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.10,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...