Jump to content

[SOLVED] strpos problem


s1yman

Recommended Posts

Hi,

 

I made a simple script to check what browser some one is using, at the moment it just displays what browser it is on the page, but I'm having problems with opera. My code is;

 

<?php
$browserinfo=$_SERVER['HTTP_USER_AGENT'] ;
print $browserinfo;
echo "<br />";

/*make lowercase*/
$browserstr=strtolower($browserinfo);
echo $browserstr;
echo "<br />";
/*If string contains "firefox" set type to firefox, if not check for other browsers*/
if (strpos($browserstr,"firefox"))
$browsertype="firefox";

elseif (strpos($browserstr,"opera"))
$browsertype="opera";

elseif (strpos($browserstr,"msie"))
$browsertype="msie";

elseif (strpos($browserstr,"safari"))
$browsertype="safari";
/*If none of the above set type to other*/
else
$browsertype="other";

echo $browsertype;
?>

 

browserstr displays "opera/9.63 (windows nt 5.1; u; en) presto/2.1.1" however, browsertype displays "other" it works fine for firefox, explorer and safari. Probably did something simple wrong.

 

Thanks in advance for any help.

Link to comment
https://forums.phpfreaks.com/topic/145068-solved-strpos-problem/
Share on other sites

Use stripos();

 

example...

 


<?php

function get_agent ()
{
$agent = 'other';

if ( false !== stripos ( $_SERVER['HTTP_USER_AGENT'], 'safari' ) )
{
	$agent = 'safari';
}
else if ( false !== stripos ( $_SERVER['HTTP_USER_AGENT'], 'opera' ) )
{
	$agent = 'opera';
}
else if ( false !== stripos ( $_SERVER['HTTP_USER_AGENT'], 'firefox' ) )
{
	$agent = 'firefox';
}
else if ( false !== stripos ( $_SERVER['HTTP_USER_AGENT'], 'msie' ) )
{
	$agent = 'msie';
}
else if ( false !== stripos ( $_SERVER['HTTP_USER_AGENT'], 'mozilla/4' ) || false !== stripos ( $_SERVER['HTTP_USER_AGENT'], 'mozilla/5' ) )
{
	$agent = 'mozilla';
}

return $agent;
}

/* usage */

echo get_agent ();

?>

  • 1 month later...

Archived

This topic is now archived and is closed to further replies.

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