SitePoint Sponsor |
|
User Tag List
Results 1 to 8 of 8
Thread: Help - I need Opera detection
-
Sep 15, 2003, 05:23 #1
- Join Date
- Jun 2001
- Location
- London
- Posts
- 138
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Help - I need Opera detection
Hi All,
I need to do a quick test to see if a users browser is Opera. I fully understand that the end user can change the headers so that it masks itself, but I would still like to do an automatic detection if possible.
Doing a quick search of this forum and PHP.net I have managed to uncover some stuff, but basically all scripts are doing detection for all browsers, and I only need Opera.
I'm no php scripter, but I put this together copying bits and pieces (although it does not work!)
PHP Code:<?php
If ( strpos ($HTTP_USER_AGENT,"Opera" ) == "1")
$browser = "opera.css";
$type = "Opera";
else
$browser = "gecko.css";
$type = "Not Opera";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<?php
echo("<link rel="stylesheet" href="$browser" type="text/css" media="all" />");
?>
<title>Detect Opera</title>
</head>
<body>
<h2>This browser is:-</h2>
<?php
echo ("<p>$type</p>");
?>
</body>
</html>
Thanks for any help!Martin
-
Sep 15, 2003, 06:21 #2
Use stristr() to check for Opera in the UA string. Even when identifying as another browser, the phrase "Opera" always appears in the user agent string. However, if you use standard HTML and CSS you wouldn't need to send different stylesheets based on user agents!
-
Sep 15, 2003, 06:34 #3
- Join Date
- Jun 2001
- Location
- London
- Posts
- 138
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Yes this is true, unfortunatley Opera 7 has a bug that if you use
position: fixed;
then you do not get a horizontal scroll bar, and guess what I need that scroll bar! (this bug only appears in Opera 7).
Hence I want to send a slightly modified css for Opera. Thanks for the advance I'll give it a go.Martin
-
Sep 15, 2003, 07:16 #4
- Join Date
- Jun 2001
- Location
- London
- Posts
- 138
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
O.K. Tried replacing 'strpos' with 'stristr' but now getting no page at all, I suspect syntax error? Here is code currently
PHP Code:<?php
**** If ( stristr ($HTTP_USER_AGENT ,"Opera" ) == "1" )
******** $browser ="opera.css" ;
******** $type ="Opera" ;
****
****else
****
******** $browser ="gecko.css" ;
******** $type ="Not Opera" ;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
********"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
****<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
****
**** <?php
**** echo( "<link rel=" stylesheet " href=" $browser " type=" text /css " media=" all " />" );
**** ?>
****
****<title>Detect Opera</title>
</head>
<body>
<h2>This browser is:-</h2>
<?php
**** echo ( "<p>$type</p>" );
?>
****
</body>
</html>Martin
-
Sep 15, 2003, 07:18 #5
- Join Date
- Jun 2001
- Location
- London
- Posts
- 138
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
ignore the **** in the above post, these are not in my code!!
Martin
-
Sep 15, 2003, 16:17 #6Code:
If ( stristr ($HTTP_USER_AGENT ,"Opera" ) == "1" )
What you can do is search for the existence of opera at all and work accordingly:
PHP Code:if (stristr($_SERVER['HTTP_USER_AGENT'], "Opera")) {
//code for true condition goes here.
}
-
Sep 16, 2003, 03:59 #7
- Join Date
- Jun 2001
- Location
- London
- Posts
- 138
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hi Vgarcia,
Thanks for your help with this but I am still getting no where. Currently my code reads:
PHP Code:<?php
if (stristr($_SERVER['HTTP_USER_AGENT'], "Opera")) {
//code for true condition.
$browser = "Opera" ;
}****
else {
** //code for false condition.*
$browser = "Not Opera" ;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Detect Opera</title>
</head>
<body>
<h2>This browser is:-</h2>
<?php
echo ("<p>$browser</p>");
?>
</body>
</html>Martin
-
Sep 16, 2003, 04:22 #8
- Join Date
- Jun 2001
- Location
- London
- Posts
- 138
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
O.K. finally got it working, thanks for your help vgarcia. Just for anyone else interested, this code works (and will load in a different css file if required.
PHP Code:<?php
if (stristr($_SERVER['HTTP_USER_AGENT'], "Opera")) {
//code for true condition
$browser = "Opera";
$css = "opera.css";
}
else {
//code for false condition
$browser = "Not Opera";
$css = "gecko.css";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<?php
echo("<link rel=\"stylesheet\" href=\"$css\" type=\"text/css\" media=\"all\" />");
?>
<title>Detect Opera</title>
</head>
<body>
<h2>This browser is:-</h2>
<?php
echo ("<p>$browser</p>");
?>
</body>
</html>Martin
Bookmarks