PDA

Prikaži potpunu verziju : Prepoznavanje browsera koji posetilac koristi


vladakg
26.12.2010, 11:07
Da li zna neko kako mogu da napisem kod, koji bi mi prepoznavao koji browser posetilac koristi, posto imam jedan slideshow koji se ne vidi u mrtvom explorer-u. Ja bi hteo da napisem u kodu, da ako posetilac koristi neki od normalnih pretrazivaca da prikaze taj slideshow, a ako je u pitanju explorer da prikaze staticnu sliku. Da li je ovo moguce? Znaci odradio bih ovo pomocu if, else samo kako da prepoznam da li je explorer u pitanju.

Patton
26.12.2010, 14:10
http://php.net/manual/en/function.get-browser.php

vladakg
26.12.2010, 14:26
<?php
function using_ie()
{
$u_agent = $_SERVER['HTTP_USER_AGENT'];
$ub = False;
if(preg_match('/MSIE/i',$u_agent))
{
$ub = True;
}

return $ub;
}

function ie_box() {
if (using_ie()) {
?>
<div class="iebox">
This page is not designed for Intenet Explorer. If you want to see this webpage as intended, please use a standards compliant browser, such as <a href="http://www.google.com/chrome">Google Chrome</a>.
</div>
<?php
return;
}
}
?>

Ubacio sam ovo ali ne radi, ne izbacuje nista u IE

vladakg
26.12.2010, 14:31
Snasao sam se, evo resenja :


<?php
function getBrowser()
{
$u_agent = $_SERVER['HTTP_USER_AGENT'];
$bname = 'Unknown';
$platform = 'Unknown';
$version= "";

//First get the platform?
if (preg_match('/linux/i', $u_agent)) {
$platform = 'linux';
}
elseif (preg_match('/macintosh|mac os x/i', $u_agent)) {
$platform = 'mac';
}
elseif (preg_match('/windows|win32/i', $u_agent)) {
$platform = 'windows';
}

// Next get the name of the useragent yes seperately and for good reason
if(preg_match('/MSIE/i',$u_agent) && !preg_match('/Opera/i',$u_agent))
{
$bname = 'Internet Explorer';
$ub = "MSIE";
}
elseif(preg_match('/Firefox/i',$u_agent))
{
$bname = 'Mozilla Firefox';
$ub = "Firefox";
}
elseif(preg_match('/Chrome/i',$u_agent))
{
$bname = 'Google Chrome';
$ub = "Chrome";
}
elseif(preg_match('/Safari/i',$u_agent))
{
$bname = 'Apple Safari';
$ub = "Safari";
}
elseif(preg_match('/Opera/i',$u_agent))
{
$bname = 'Opera';
$ub = "Opera";
}
elseif(preg_match('/Netscape/i',$u_agent))
{
$bname = 'Netscape';
$ub = "Netscape";
}

// finally get the correct version number
$known = array('Version', $ub, 'other');
$pattern = '#(?<browser>' . join('|', $known) .
')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
if (!preg_match_all($pattern, $u_agent, $matches)) {
// we have no matching number just continue
}

// see how many we have
$i = count($matches['browser']);
if ($i != 1) {
//we will have two since we are not using 'other' argument yet
//see if version is before or after the name
if (strripos($u_agent,"Version") < strripos($u_agent,$ub)){
$version= $matches['version'][0];
}
else {
$version= $matches['version'][1];
}
}
else {
$version= $matches['version'][0];
}

// check if we have a number
if ($version==null || $version=="") {$version="?";}

return array(
'userAgent' => $u_agent,
'name' => $bname,
'version' => $version,
'platform' => $platform,
'pattern' => $pattern
);
}

// now try it
$ua=getBrowser();
if ($ua['name'] == 'Internet Explorer') {
echo "Batalite ovaj browser i predjite na neki normalan";
} else {
echo "Koristite normalan browser";
}
?>

ZoNi
26.12.2010, 19:32
// now try it
$ua=getBrowser();
if ($ua['name'] == 'Internet Explorer') {
echo "Batalite ovaj browser i predjite na neki normalan";
} else {
echo "Koristite normalan browser";
}
?>

Kada bih naisao na sajt koji mi napise nesto ovako, pomenuo bih pola familije webmasteru i nikad se vise ne bih vratio na tu web stranu.

vladakg
26.12.2010, 20:54
To je primer samo, ali veoma je i poucan