Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple Browser Detect

Status
Not open for further replies.

tyrannus

MIS
Apr 6, 2006
115
US
Hey all, once again I turn to the experts for some help. Y'all have been very useful and a great help in my learning php endeavors. The job I have pretty much pushed me head first into php with minimal knowledge so I need to come here at times for some help.

I have written a script to help determine the browser and put that into a variable. I then want to use this variable to help determine which section of code to run..for example if ($browser == IE){ do this code}else{ do this code}

Here is the script I wrote:

<?php
$useragent = $_SERVER[‘HTTP_USER_AGENT’];

if (preg_match(‘|MSIE ([0-9].[0-9]{1,2})|’,$useragent,$matched)) {
$browser_version=$matched[1];
$browser = ‘IE’;
} elseif (preg_match( ‘|Opera ([0-9].[0-9]{1,2})|’,$useragent,$matched)) {
$browser_version=$matched[1];
$browser = ‘Opera’;
} elseif(preg_match(‘|Firefox/([0-9\.]+)|’,$useragent,$matched)) {
$browser_version=$matched[1];
$browser = ‘Firefox’;
} elseif(preg_match(‘|Safari/([0-9\.]+)|’,$useragent,$matched)) {
$browser_version=$matched[1];
$browser = ‘Safari’;
} else {
// browser not recognized!
$browser_version = 0;
$browser= ‘other’;
}

echo 'browser: ' . $browser $browser_version;
?>

When I run it I get this error but cannot find what it is talking about:

Parse error: parse error, unexpected '[', expecting ')' in c:\program files\apache group\Apache\htdocs\browser_detect2.php on line 4


If y'all can even just point me in the right direction with out giving me the straight out answer (this way I learn something) I would be grateful!!
 
There are doublequotes ("), singlequotes (', also known as the apostrophe) and backticks (`). The backtick is in a US-standard keyboard usually the one with the tilde on it. PHP uses backticks for external program execution.

Your post includes:

useragent = $_SERVER[[red]‘[/red]HTTP_USER_AGENT’];

(the backtick is in red)

It looks to me like you're trying to enclose your string in one backtick and one singlequote, which would very likely be the cause of your error.



Want the best answers? Ask the best questions! TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top