An Introduction to PHP

Server Variables

All servers maintain a set of variables that provide information such as where the user come from, and other useful information. You can access these variables by name in PHP. For example, if you wanted to know the link that the user clicked to get to the current page, you could use the $_SERVER["HTTP_REFERER"] server variable. The following example lists the referer, and the visitor's browser.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
            "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Server Variables</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<p>
<?php
    print "Referer: " . $_SERVER["HTTP_REFERER"] . "<br>";
    print "Browser: " . $_SERVER["HTTP_USER_AGENT"]. "<br>";
?>
</p>
</body>
</html>

List of Server Variables

The $_SERVER array stores all of the server variables that can be accessed. The $_SERVER variable is an associative array that you could iterate using the foreach construct. The following example lists each of the variables, and the associated value.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
            "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>List of Server Variables</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<h1>List of Server Variables</h1>
<p>
<?php
    foreach($_SERVER as $key=>$value)
        print $key . " = " . $value . "<br>";
?>
</p>
</body>
</html>

You might also like...

Comments

About the author

Gez Lemon United Kingdom

I'm available for contract work. Please visit Juicify for details.

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“There are only 3 numbers of interest to a computer scientist: 1, 0 and infinity”