SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot martin's Avatar
    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>
    I'm trying to detect the user string and if it contains Opera then I will echo out an alternative, slightly modified css file. I understand this will not work if the user is masking as IE for example.

    Thanks for any help!
    Martin

  2. #2
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    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!

  3. #3
    SitePoint Zealot martin's Avatar
    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

  4. #4
    SitePoint Zealot martin's Avatar
    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>
    Any other ideas? - Thanks!
    Martin

  5. #5
    SitePoint Zealot martin's Avatar
    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

  6. #6
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Code:
    If ( stristr ($HTTP_USER_AGENT ,"Opera" ) == "1" )
    stristr() won't return a number, it will return the remainder of the string (the "haystack") from the phrase (the "needle") you're looking for. More info here.

    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.


  7. #7
    SitePoint Zealot martin's Avatar
    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>
    But there must be an error as I am not getting anything appearing in the browser. If I cut out the detection php at the top of the page at least I get the html part of the page, so I am assuming I have some syntax error in the php code? Sorry to be a pain but I really would like to understand why this is not working!
    Martin

  8. #8
    SitePoint Zealot martin's Avatar
    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>
    thanks for all the help!
    Martin


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •