Server Environment Variables Now let's look at the information that PHP allows you to find out from your web server. The $_SERVER super-global array contains a number of elements that give information about the web server environment during the current page request. To see the full list within the context of a script, you execute this statement at any time: print_r($_SERVER); The examples in this section are common to most web servers. However, some servers may not support all the values shown or may use different names. You can always refer to the output from the previous statement to check which values are available in your script. Script Information The name of the current script can be found in $_SERVER["SCRIPT_NAME"]. Knowing this name can be useful if you want to create a form that submits to itself but whose filename you might want to change in the future. You could use the following tag: <FORM ACTION="<?php print $_SERVER["SCRIPT_NAME"];?>" METHOD=POST> Similar to SCRIPT_NAME is the REQUEST_URI element, which contains the full uniform resource identifier of the page request. This consists of the full path to the current script, including the question mark and values in the query string, if there are any. The query string is not included as part of the SCRIPT_NAME element, but you can access it on its own as $_SERVER["QUERY_STRING"]. If you want to find the domain name under which a script is running, you can look at $_SERVER["HTTP_HOST"]. Your web server might be set up with several alias domains, and this provides a way to see which domain name a visitor is viewing your pages on. User Information The HTTP_USER_AGENT element contains a string that identifies the user's web browser software and operating system. It might look like one of the following: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0 Lynx/2.8.5dev.7 libwww-FM/2.14 SSL-MM/1.4.1 OpenSSL/0.9.7a These three examples correspond to Internet Explorer, Mozilla Firefox, and Lynx, respectively. Notice that both Internet Explorer and Firefox report themselves as Mozilla browsers, so to find out specifically which program a user has, you have to look further into the string. The following condition can be used to produce different output for Internet Explorer than for Firefox: if (strstr($_SERVER["HTTP_USER_AGENT"], "MSIE")) { echo "You are using Internet Explorer"; } elseif (strstr($_SERVER["HTTP_USER_AGENT"], "Firefox")) { echo "You are using Firefox"; } else { echo "You are using some other web browser"; } You may need to use this occasionally because of differences in the browsers' implementations of DHTML or JavaScript. The REMOTE_ADDR attribute contains the IP address that the hit to the web server came from. It should either be the IP address of the user's computer or the user's ISP's web cache. You might want to use the remote IP address for logging or security. If the REMOTE_ADDR value is a web cache, the element HTTP_X_FORWARDED_FOR is also present, and it contains the IP address of the user's computer. If the user has logged in by using basic HTTP authentication, you can also find out his or her username by looking at the value in $_SESSION["REMOTE_USER"]. Server Information Other elements in $_SERVER allow you to access various values related to the web server configuration. For instance, $_SERVER["SERVER_NAME"] corresponds to the ServerName Apache directive. This is the primary name of this web server, but not necessarily the domain name the scripts are being accessed from; it might not be the same as $_SERVER["HTTP_HOST"]. Similarly, $_SESSION["SERVER_ADMIN"] holds the webmaster's email address that is set in the ServerAdmin directive. The SERVER_ADDR and SERVER_PORT elements contain the IP address and port number of the machine the web server is running on. Checking $_SERVER["REQUEST_METHOD"] reveals whether a GET or POST method was used to pass values to the script. Finally, if you are working on a shared web host or someone else's web server and want to see what web server software version that person is running, you can check $_SERVER["SERVER_SOFTWARE"]. This value is the same as the one transmitted in the Server header at the beginning of this lesson, and it is similar to the following: Apache/1.3.29 (Unix) mod_gzip/1.3.26.1a PHP/4.3.9 mod_ssl/2.8.16 OpenSSL/0.9.7c Summary In this lesson you have learned how to communicate with a web server. In the next lesson you will learn about filesystem access using PHP. Lesson 17. Filesystem Access In this lesson you will learn how to access a web server's filesystem by using PHP and how to read and write files from within a script. Managing Files Let's examine how PHP allows you to work with files stored on a web server's hard disk. File Permissions Before you can perform any filesystem access from PHP, you have to consider the permission settings for the files you want to work with. This section deals primarily with the file permissions system on Unix/Linux systems, but the same considerations apply to all platforms. Usually your web server will be running under either the apache or nobody username, yet your web documents and PHP scripts will be owned by your actual system user. Unless the web server user has permissions to access your files, any access attempts will fail. To grant to all users read-only access to a file, you use the chmod command, which sets the read flag (r) on the file for all users other than the current one (o): $ chmod o+r filename You can also set global read/write permissions on a file by using chmod as follows: $ chmod o+rw filename Refer to man chmod for full details and more examples. If a file has global permissions, the web server will be able to access it. However, any other user on your system will also have full access to these files. In some situations you might prefer to change the ownership on a file to the apache user rather than grant global write access. The superuser does this by using the chown command: # chown apache filename The PHP function chmod can also be used to alter file permissions. It takes two arguments: a filename and a mode. The mode argument can be either the string type, such as o+rw, or the numeric type, such as 0644, as long as the number is prefixed with a zero. Using chmod If you are familiar with the system command chmod, you may be used to giving a three-digit number as the mode argument. When run from the shell, chmod assumes that an octal value is given, whether or not it is prefixed with a zero; for example, chmod 644 is the same as chmod 0644. In PHP, however, the leading zero is always required in the argument to chmod. Getting Information About a File PHP provides a wide range of functions for getting information about a file on your system. The simplest, and one you may use often, is file_exists, which simply tells you whether there is a file with the given name argument. The file_exists function returns TRue if any item on the filesystem has the given name, even if it is a directory or a special file type. The argument may contain a path, as shown here: if (file_exists("/home/chris/myfile")) { echo "The file exists"; } else { echo "The file does not exist"; } A number of other functions allow you to test certain attributes of a file. These are shown in Table 17.1. Table 17.1. Functions for Testing Attributes of a File Function Description is_executable Checks whether the file has the executable attribute. is_readable Checks whether the file is readable. is_writeable Checks whether the file is writeable. is_link Checks for a symbolic link. is_file Checks for a real file, not a link. Yet other functions return information about the file itself. These are shown in Table 17.2. Table 17.2. Functions That Find Information About a File Function Description fileatime Checks the time of the last file access, as a Unix timestamp. filectime Checks the time of file inode creation. filemtime Checks the time of the last modification to the file. fileowner Checks the user ID of the file owner. filegroup Checks the group ID of the file. fileinode Checks the inode number of the file. fileperms Checks the file's permission settings as an octal value (for example, 0644). filesize Checks the size of the file in bytes. filetype Checks the type of the file (fifo, char, dir, block, link, or file). Moving and Copying Files Assuming that you have permission to do so, you can perform a file copy, move, or delete operation from PHP. The functions for these actions are copy, rename, and unlink, respectively. The copy and rename functions take two argumentsthe source and destination filenameswhereas unlink takes a single filename. File Paths You should be particularly careful when performing file operations from PHP, particularly when deleting. You need to always make sure you know what the current working directory is, or give a full path to the target file. Working with Filenames The functions basename and dirname provide an easy way to dissect a string into a filename and path, respectively. You might use these functions to find out the base filename when a full path is given or to find the pathname if you want a file created with other files in the same place as a known filename. The basename function returns everything from the last slash character in the string to the end, whereas dirname returns the portion of the string before this slash. The realpath function takes a pathname argument and returns its absolute pathname. Any symbolic links in the path are resolved to their actual location on disk, and any references to the current or parent directory using . or are removed. If you want to write to a temporary file, you can use the tempnam function to generate a unique temporary filename. It takes two argumentsa directory name and a filename prefix. The prefix argument is required but can be an empty string. The following statement generates a temporary filename in /tmp with no prefix: $filename = tempnam("/tmp", ""); . Now let's look at the information that PHP allows you to find out from your web server. The $_SERVER super-global array contains a number of elements that give information about the web. is always required in the argument to chmod. Getting Information About a File PHP provides a wide range of functions for getting information about a file on your system. The simplest, and. file, not a link. Yet other functions return information about the file itself. These are shown in Table 17.2. Table 17.2. Functions That Find Information About a File Function Description