Team LiB
Previous Section Next Section

Displaying Browser-Specific HTML

In the previous chapter, you learned to retrieve and print the HTTP_USER_AGENT environment variable to the screen. In this chapter, you do something a bit more interesting with the value of HTTP_USER_AGENT, and that's printing browser-specific HTML.

However, having seen some of the possible values of HTTP_USER_AGENT in the last chapter, you can imagine that there are hundreds of slightly different values. So it's time to learn some basic pattern matching.

You'll use the preg_match() function to perform this task. This function needs two arguments: what you're looking for, and where you're looking:

preg_match("/[what you're looking for]/", "[where you're looking]");

This function returns a value of true or false, which you can use in an ifelse block to do whatever you want. The goal of the first script is to determine whether a web browser is Microsoft Internet Explorer, Netscape, or something else.

Within the value of HTTP_USER_AGENT, Netscape always uses the string Mozilla to identify itself. Unfortunately, the value of HTTP_USER_AGENT for Microsoft Internet Explorer also uses Mozilla to show that it's compatible. Luckily, it also uses the string MSIE, so you can search for that. If the value of HTTP_USER_AGENT doesn't contain either Mozilla or MSIE, chances are very good that it's not one of those web browsers.

  1. Open a new file in your text editor and start a PHP block, and then use getenv() to place the value of HTTP_USER_AGENT in a variable called $agent:

    <?
    $agent = getenv("HTTP_USER_AGENT");
    
  2. Start an ifelse statement to find which of the preg_match() functions is true, starting with the search for MSIE:

    if (preg_match("/MSIE/i", "$agent")) {
         $result = "You are using Microsoft Internet Explorer.";
    }
    
    
  3. Continue the statement, testing for "Mozilla":

    Click To expand
  4. Finish the statement by defining a default:

    else {
         $result = "You are using $agent";
    }
    
  5. Close your PHP block and add some HTML to begin the display:

    ?>
    <HTML>
    <HEAD>
    <TITLE>Browser Match Results</TITLE>
    </HEAD>
    <BODY>
    
  6. Type the following PHP code to print the result of the ifelse statement:

    <? echo "<P>$result</P>"; ?>
    
  7. Add some more HTML so that the document is valid:

    </BODY>
    </HTML>
    
  8. Save the file with the name browsermatch.php, and place this file in the document root of your web server.

  9. Open your web browser and type http://127.0.0.1/browsermatch.php.

Depending on the web browser you use, you might see a result such as shown in the following two figures.

Click To expand

If you're using neither Netscape nor Microsoft Internet Explorer, the actual value of HTTP_USER_AGENT will be printed.

Various flavors of Microsoft Internet Explorer (MSIE) account for approximately 78% of web browsers in use, whereas versions of Netscape (NS) take up about 15%. Throw in the die-hard Lynx, Opera, Konquerer, and other users to reach 100%.

Click To expand

Although a 80/20 split might seem like a majority, if 500 million people have access to the Internet, 100 million non-MSIE users is a huge number to consider when developing a good website. HotWired maintains a browser reference at http://hotwired.lycos.com/webmonkey/reference/browser_chart/, which shows you some of the differences between the major browsers. In the next section, you'll take into consideration how not all platforms are created equal, and in fact might not display HTML similarly, either.


Team LiB
Previous Section Next Section