Page 486 - Beginning PHP 5.3
P. 486

Part III: Using PHP in Practice
                  Using a PEAR Package

                   To use a PEAR package in your script, you first need to include the package file in the script and then
                 access the package ’ s classes and methods as required.
                   As mentioned earlier, PEAR package files are installed in the PEAR path. Usually your PHP include path
                 contains the PEAR path, among others. This means that you can include a PEAR package simply by
                 referencing the path to the package file relative to the PEAR path.

                   For example, the   Net_UserAgent_Detect  package is accessed by including the file  Net/UserAgent/
                Detect.php :

                    require_once( “Net/UserAgent/Detect.php” );

                   You can then create a new  Net_UserAgent_Detect  object with:
                    $detect = new Net_UserAgent_Detect();

                   Generally speaking, to get the path to the package file, replace any underscores ( _ ) in the package name
                 with slashes (  / ) and add  .php  to the end.



              Try It Out     Detecting the Visitor’s Browser
                Now that you know how to install and access a PEAR package, try writing a script that uses a
                package. In this example you use the Net_UserAgent_Detect package to write a simple “browser
                sniffer” script that displays the user’s browser name and operating system name.
                First, install the Net_UserAgent_Detect package, if you haven’t already, by following the
                instructions in the previous section. For example:

                    pear install --alldeps Net_UserAgent_Detect

                Now save the following script as browser_sniffer.php in your document root folder.

                    <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
                     “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
                    <html xmlns=”http://www.w3.org/1999/xhtml” xml:lang=”en” lang=”en”>
                      <head>
                        <title>Browser Information</title>
                        <link rel=”stylesheet” type=”text/css” href=”common.css” />
                      </head>
                      <body>
                        <h1>Browser Information</h1>

                    <?php

                    require_once( “Net/UserAgent/Detect.php” );

                    $detect = new Net_UserAgent_Detect();
                    echo “<p>You are running “ . $detect->getBrowserString();



              448





                                                                                                      9/21/09   9:14:49 AM
          c15.indd   448                                                                              9/21/09   9:14:49 AM
          c15.indd   448
   481   482   483   484   485   486   487   488   489   490   491