WordPress.org

Ready to get started?Download WordPress

Forums

PHP Browser Detection
Opera, Safari and Chrome on Windows 7 (7 posts)

  1. endolil
    Member
    Posted 3 years ago #

    Hi there,

    here is what I am trying to do:

    // browser detection based on  http://wordpress.org/extend/plugins/php-browser-detection/
    	if (function_exists('php_browser_info')) {
    
    		$browserInfo = php_browser_info();
    
    		$browser = $browserInfo[browser];
    		$version = $browserInfo[version];
    		$platform = $browserInfo[platform];
    
    		echo '<div id="browser-os">' . $browser . ' (' . $version . ') - ' . $platform .'</div>';
    	}

    Unfortunatly I only see the result in Firefox and Internet Explorer. Safari, Opera and Chrome seems to refuse displaying the requested information.

    Does anybody has an idea why?

  2. MartyThornley
    Member
    Plugin Author

    Posted 3 years ago #

    This plugin relies on a text file that holds browser info. Some browsers, like the latest Safari(5) and I am guessing chrome are not in there. I really only built it to help detect Internet Explorer and to serve alternate content for iPhones.

    There is a newer version that I could update it to, but it has not been a priority for me. One of these days I will try to update it though.

    In the meantime, you can try

    $browserInfo = php_browser_info();
    print_r($browserInfo);

    This will print out the array of info returned from the function to give you an idea of what it has to work with.

  3. endolil
    Member
    Posted 3 years ago #

    Thank you Marty,

    the thing is that I want to detect a Mac Safari and I am working on a Windows maschine.

    In this case your code will not help but thank you for sharing.

    It would be great if there is somebody updating that file but on the other side it is doubtful if a browser detection makes sense in terms of detecting a version. For Internet Explorer it might make sense and probably also for modern mobile devices like the iPhone.

    I just wanted to change the text size for the Mac Safari (based on CSS) because it displays the websites that I am making alsways a bit bigger what looks lousy. I spend more than 3 hours looking at this topic and in the end I gave up because the story seems to be an endless one and not really promising at all.

    I was even checking jQuery browser detection but lately they changed their concept from browser- to feature-detection and that does not helps in my case either, even when it is a good concept.

    Anyway,
    I let it go for now and accept Safari to be different. That makes my life easier.

    ... edit ...

    I was to curious to let it completly go, even when I do not want to work with it now:

    http://techpatterns.com/forums/about304.html

    And if somebody wants the full programm ;)
    User Agent String.Com

    For testing this might be helpful:
    User Agent Switcher 0.7.2 for Firefox

  4. dertien
    Member
    Posted 3 years ago #

    Hello there,

    First of all, vielen dank, for the lovely plugin.

    Now I don't know how to test for mac, but here's maybe part of the solution, since I needed to test for Chrome and Opera as well.This is what I did and it works.

    1) Go to http://browsers.garykeith.com/downloads.asp and download the lite version
    (lite_php_browscap.ini) for PHP though, mind you. (it's the 4th one from the top)

    2) Go to your plugins directory and rename the file 'php_browscap.ini' that came with Marty's plugin and rename it to 'php_browscap.bak'. Then copy the downloaded file from step 1 to marty's plugin directory and rename it 'php_browscap.ini'. 'php_browscap.bak' and 'php_browscap.ini' should be in the same directory.

    It has the latest versions of chrome in them.

    3) Open header.php of your favorite theme and copy this above the </title> section. (or use the PHP example on Marty's site: http://martythornley.com/downloads/php-browser-detection/

    Here is my code which should give the proper browser name in the different tested browsers.

    <?php
    
    if (is_firefox()) {
    echo 'This is Firefox';
    };
    
    if (is_chrome()) {
    echo 'This is Chrome';
    };
    if (is_IE7()) {
    echo 'This is $#@! IE 7';
    }
    
    if (is_opera()) {
    echo 'This is Opera';
    }
    
    ?>

    4) save the document and load your site with localhost/yoursite in the different browsers with the plugin and the ammended theme activated.

    Another note:
    Find the different names by opening the 'php_browscap.ini' file in notepad and CTRL-F for different versions of Safari.

    Hope this helps.

  5. endolil
    Member
    Posted 3 years ago #

    Thank you dertien,

    for your sharing this partly solution.
    I am not sure if it works for me.

    Actually I try to reduce using plugins for things that I can manage with the functions.php or some simple includes into the theme files.

    I will not use the browser checking very intense because I wanna use clean CSS and HTML code that works in the major browsers and some special hacks or features ;) I will only use if really neccessary. I wanna keep it simple, so I will look for a script that gives me the control to check browser type and operating system and that's it for now.

    When I try to use the phpbrowsercap.php I have no idea how to use it because it is a class and I am not that deep in PHP so that I know how I can work with a class. I also do not want to hack it into the plugin mentioned above - I want to do something that I really understand and that does not need a plugin.
    This is a new decision and it is a good one. :)

    I will let you know if and when I found a simpler solution.

    Warm regards,
    Sebastian

  6. MartyThornley
    Member
    Plugin Author

    Posted 3 years ago #

    Just updated this plugin. As @dertien pointed out, the problem was mainly an outdated browscap.ini file. The latest (as of Sept 16 2010) has been added.

    @zabdesign - Definitely understand not wanting extra plugins. This is a very light one though. You could easily just add the functions into a functions.php file. Just make sure to include the browscap.ini file and make sure you change the line in the plugin that looks for it.

    The benefit of adding it as a plugin is that you can upgrade it, when I do. :)

    It now recognizes:

    Firefox
    Safari (even 5)
    Chrome
    Opera
    IE
    iPad
    iPod
    iPhone

    you can also check for a specific version like this:

    if (is_firefox('3'))
    if (is_safari('5'))

    Tests for javascript, css, and cookies.

    For anything I didn't build in you can use

    $browserInfo = php_browser_info();

    And that will return an array of all the info.

    See the description on the plugin page for more.

  7. Clicknathan
    Member
    Posted 2 years ago #

    Here's a way to check OS & browser:

    <?php
    	$userAgent = $_SERVER['HTTP_USER_AGENT'];
    	$os = "Mac OS X";
    	$browser  = "MSIE";
    
    	$osx = strpos($userAgent, $os); // Checks to see if the operating system is OS X
    	$ie = strpos($userAgent, $browser); // checks to see if the browser is IE
    
    	if ($osx == true)  { ?>
    
    	<style type="text/css">
                   ADD YOUR CSS HERE
    	</style>
    
    	<?php }
    
    	elseif ($ie == true)  { ?>
    
    	<style type="text/css">
    
            ADD YOUR CSS HERE
    
    	</style>
    
    <?php } ?>

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic