The $_SERVER Array

The $_SERVER array contains elements set by your script’s context, usually the server. There is no guarantee that any or all of the common elements will be set in it. If you are running PHP as a server module, however, it is likely that you will find at least the elements summarized in Table 10.2. They can be very useful in providing additional information about the context of a user request.

In Listing 10.1, we loop through the $_SERVER array, printing the results to the browser.

Listing 10.1. Looping Through the $_SERVER Array
 1: <!DOCTYPE html PUBLIC
 2:     "-//W3C//DTD XHTML 1.0 Strict//EN"
 3:     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 4: <html>
 5: <head>
 6: <title>Listing 10.1 Looping through the $_SERVER array</title> ...

Get Sams Teach Yourself PHP in 24 Hours, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.