If you’re considering the use of PHP Server Environment Variables in your web application, but you are not certain which $_SERVER variable will produce the information you need, you are not alone. It’s not uncommon for the PHP web application developer to cross reference $_SERVER variable data during the development process.
Busting the Myth of PHP $_SERVER
$_SERVER can be difficult to understand because, though the qualities of the predefined Array are set as part of the programming language itself†, many of the returned values are dependent upon the file where it’s used, and the directory in which that file resides, relevant to the http server root. The previous sentence may make more sense after some experimentation. To master the power of Environment Variables, you might find it best to simply create a few documents containing values produced by a few of the more basic $_SERVER variables. Ensure that the document can be viewed in a browser without causing errors in the output, then save the document in your root directory, and again after placing it in different directories, each a deeper distance from the root directory (e.g. http://localhost; http://localhost/dir1; http://localhost/dir1/dir2; etc.). During your experimentation with viewing $_SERVER values output from different directories, take note to precisely how those values change in relation to the directory depth of the document from the current browser view. are used Once I became more acquainted with the several available options presented by this integral PHP characteristic, I began to realize many ways in which the predefined constants available through $_SERVER can breath life into the creative process. I recently discovered a little trick to save time in determining which _SERVER variable is most appropriate for the data i wish to enter into my application.
All you need to do is simply print to screen the PHP function phpinfo()
INSIDE OF THE FILE you’re going to use, by entering the line echo phpinfo();
on the page somewhere where you’ll be able to see the full output listing. you’ll see all of the possible options for that location!
-
the Problem
_SERVER
- Do you need to pull the value from one of the predefined PHP global varialables? Don’t know what I mean? Maybe some of these look familiar to you:
- _SERVER[“PATH”]
- _SERVER[“SERVER_SIGNATURE”]
- _SERVER[“SERVER_SOFTWARE”]
- _SERVER[“SERVER_NAME”]
- _SERVER[“SERVER_ADDR”]
- _SERVER[“SERVER_PORT”]
- _SERVER[“REMOTE_ADDR”]
- _SERVER[“DOCUMENT_ROOT”]
- _SERVER[“SERVER_ADMIN”]
- _SERVER[“SCRIPT_FILENAME”]
- _SERVER[“REMOTE_PORT”]
- _SERVER[“GATEWAY_INTERFACE”]
- _SERVER[“SERVER_PROTOCOL”]
- _SERVER[“REQUEST_METHOD”]
- _SERVER[“QUERY_STRING”]
- _SERVER[“REQUEST_URI”]
- _SERVER[“SCRIPT_NAME”]
- _SERVER[“PHP_SELF”]
- _SERVER[“REQUEST_TIME”]
phpinfo()
The PHP function,
phpinfo()
is typically introduced to beginners within the first few tutorials– to demonstrate some of the basics of the Language. It is also often used as a test-page during an Apache, or localhost server configuration– to determine whether the PHP parser is functioning properly. However, if the function is completely foreign to you, I suggest that you try it now in a new document, as the information there is practically essential to gaining a thorough understanding of the fundamental building blocks of PHP development.To use the
phpinfo()
function, only the following code is needed to produce the desired output:<?php echo phpinfo(); ?>
Leave a Reply