The Host Environment


Now let's look at how PHP interacts with the web server's host environment.

Detecting the Host Platform

Because different types of systems have different sets of host commands available, if you are writing a script that could potentially be executed on different platforms, it's useful to detect what kind of web server is being used.

The constant PHP_OS contains a string that represents the operating system. The most common reason for checking this is to find out whether a script is running on a Windows platformafter all, most Unix-like systems, and even Mac OS, behave in a very similar way.

The value of PHP_OS on a Windows web server could be Windows, WINNT, or WIN32, and in the future, other values may come into existence. Therefore, to test for a Windows platform, you should perform a non-case-sensitive comparison on the first three characters of the string. The following condition shows just one of the ways you can do this:

 if (strtoupper(substr(PHP_OS, 0, 3)) == "WIN") { ... } 

Darwin Be cautious to check only that the value of PHP_OS begins with WIN, as modern versions of Mac OS report themselves as Darwin.


Environment Variables

The $_ENV super-global contains an element for each environment variable present. Environment variables are values from the underlying operating system, and those available to PHP are from the environment in which PHP and your web server is running.

The PATH environment variable provides your system with a list of locations to search for an executable program. Each location is checked in turn until the program is found or there are no more locations left to try, when an error occurs.

Finding the current value of the path is as simple as using the following statement:

 echo $_ENV["PATH"]; 

On a Unix/Linux system it may look like the following:

 /bin:/usr/bin:/usr/X11R6/bin:/home/chris/bin 

On a Windows system, however, it may look like this:

 C:\WINDOWS\system32;C:\WINDOWS 

Notice that the format is considerably different for the different operating systems. The Unix/Linux version uses colons to separate the locations and forward slashes in pathnames, and the Windows version uses semicolons and backslashes. For this reason, PHP provides the host-specific constants DIRECTORY_SEPARATOR and PATH_SEPARATOR, which enable you to find the appropriate symbols to use for each of these.

In many cases, resetting the PATH value is specific to the underlying platform; for instance, even if you use the correct PATH_SEPARATOR constant, C:/WINDOWS will not exist on a Linux server. However, this allows you to add the current working directory, or one relative to it, to the path fairly easily.

The following example adds the directory bin, relative to the current location, to the start of the system path:

 $newpath = getcwd() . DIRECTORY_SEPARATOR . "bin" .              PATH_SEPARATOR . $_ENV["PATH"]; putenv("PATH=$newpath"); 

The putenv function takes a single argument in which an environment variable is assigned its new value. This change is not permanent, and the new value is remembered only until the script ends.

Time Zones

The TZ environment variable contains the server's time zone setting. By overriding this value, you can display the time in another part of the world without needing to know the correct offset or perform any date arithmetic.

Most major cities or regions of the world have a value for TZ that is easy to remember or work out (for instance, Europe/London, US/Pacific). It can also be a value relative to Greenwich Mean Time or some other common time zone, such as GMT-8 or EST. On most systems, you can find the available time zones by looking at the items in /usr/share/zoneinfo.

The script in Listing 18.1 displays the current time in several locations around the globe.

Listing 18.1. Using the TZ Environment Variable to Change Time Zone
 <?php $now = time(); $original_tz = $_ENV["TZ"]; echo "The time now is " . date("H:i:s", $now) . "<br>"; putenv("TZ=US/Pacific"); echo "The time on the US West Coast is " .              date("H:i:s", $now) . "<br>"; putenv("TZ=Europe/Paris"); echo "The time in France is " . date("H:i:s", $now) . "<br>"; putenv("TZ=Australia/Sydney"); echo "The time in Sydney is " . date("H:i:s", $now) . "<br>"; putenv("TZ=Asia/Tokyo"); echo "The time in Tokyo is " . date("H:i:s", $now) . "<br>"; putenv("TZ=$original_tz"); ?> 

Note that Listing 18.1 begins by storing the current time zone value so that it can be restored after you are done changing the value.

Storing the Time The timestamp is saved to $now at the start of Listing 18.1 so that the same value can be passed to each date function. Although the second argument to date can be omitted, if it is omitted, it is possible that the script execution could take place as a second ticks over, which would produce confusing output.




    Sams Teach Yourself PHP in 10 Minutes
    Sams Teach Yourself PHP in 10 Minutes
    ISBN: 0672327627
    EAN: 2147483647
    Year: 2005
    Pages: 151
    Authors: Chris Newman

    Similar book on Amazon

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net