Hudzilla.org - the homepage of Paul Hudson
Contents > Simple variables and operators Wish List | Report Bug | About Me ]

3.9     Pre-set variables

This is NOT the latest copy of this book; click here for the latest version.

Before you even get control in your script, PHP has set a number of variables for you containing information about the server, the environment, and the request from your visitor. These are stored in the superglobal arrays for you, and you can get a fairly complete list of what is available by using the phpinfo() output.

The most commonly used variables, all of which are stored in the $_SERVER superglobal, are as follows:

Name

Value

HTTP_REFERER

If the user clicked a link to get the current page, this will contain the URL of the previous page they were at, or it will be empty if the user entered the URL directly.

HTTP_USER_AGENT

The name reported by the visitor's browser

PATH_INFO

Any data passed in the URL after the script name

PHP_SELF

The name of the current script

REQUEST_METHOD

Either GET or POST

QUERY_STRING

Includes everything after the question mark in a GET request

Note that you need to use HTTP_REFERER and not HTTP_REFERRER. This is one of the very few misspellings ever to make it into a web standard, but is now in widespread use and so too late to change.

Of those, HTTP_REFERER and HTTP_USER_AGENT are the most important, as you can use these two to tell an awful lot about your visitor and take the appropriate action. For example:

<?php
    
if (isset($_SERVER['HTTP_REFERER'])) {
        print
"The page you were on previously was {$_SERVER['HTTP_REFERER']}<BR />";
    } else {
        print
"You didn't click any links to get here<BR />";
    }
?>

<A HREF="refer.php">Click me!</A>

When that page is loaded up in your browser by typing the URL in by hand, the "You didn't click any links to get here" text is shown because HTTP_REFERER has not been set. However, if once the page is loaded you click the "Click me!" link, the page will reload itself and this time HTTP_REFERER will be set and the new message should appear. Although it can be spoofed, HTTP_REFERER is generally a good way to make sure a visitor came from a certain page - whether you want to use that to say, "you can't download my files because you came from another site", or "welcome, Google users!" is down to you, but there is a lot of scope for ideas.

The PATH_INFO element in $_SERVER is particularly interesting, because it allows you to grab directory information specified after the script. Consider this script:

<?php
    
if (isset($_SERVER['PATH_INFO'])) {
        print
"The page you requested was {$_SERVER['PATH_INFO']}<BR />";
    } else {
        print
"You didn't request a page<BR />";
    }
?>

If you save that as pathinfo.php in your document root, try loading it up in your web browser - you should see "you didn't request a page". Now, try editing the URL so that after pathinfo.php is a filename, with as much directory information as you want. For example: www.yoursite.com/pathinfo.php/path/to/some/file.txt. Now when you load the page, you should see that extra path information printed out. This is commonly used in online filesystems, as it means that the URLs required to get to files are just the name of the script followed by the filename wanted.

Author's Note: Remember that the referrer value is set by the web browser, which means it can easily be faked. One common example of this is to edit the "hosts" file of the computer (/etc/hosts in Unix; c:\windows\system32\drivers\etc\hosts in Windows) so that the current computer is used as www.example.com. Then, J. Evil Hacker loads a simple page on their computer with a link to your "secure" script, and his browser will report that he came from example.com. As a result, you should never rely on HTTP_REFERER to be set, valid, or truthful, but it is a good start.





<< 3.8 Superglobals   3.10 References >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Raven2260@gmail.com - 07 Sep 2008

So if your using apache there is no way to use $_SERVER['REMOTE_PW']

Houdini - 07 Sep 2008

Well to edit a URL you just add to it or alter what is there to what you want. Suppose that you had a page that had a URL of http://somesite.com/admin.php To edit it to tack some information to be passed to the admin.php page you would click your mouse in the address bar of your browser and right behing the admin.php add ?text=test&action=edit

Then in your address bar the URL would now be http://somesite.com/admin.php?text=test&action=edit

then press enter and then you will see that the page has gone to the admin.php page and now using either $_GET or $_REQUEST you can retrieve the values of text and action which were passed through the URL so $_GET['text'] would equal test and $_GET['action'] would equal edit.

A PHP SOOPR NOOB - 07 Sep 2008

How do you edit a URL? I don't understand how to access the address bar and change the information displayed. Can someone give an example of:

Now, try editing the URL so that after pathinfo.php is a filename, with as much directory information as you want. For example: www.yoursite.com/pathinfo.php/path/to/some/file.txt. Now when you load the page, you should see that extra path information printed out.

Thnx.

deathGod - 07 Sep 2008

holy smokes
i uploaded the previous file to an .htaccess directory on my web server and it gave me my user name but not my password. the good thing is that in order to access that directory to view the file, i have to know my username and password first:) but i'm sure there are ways to work around that. Why didn't it show me my password, is it some security measure that php4.1 or thereabouts has built in.

try this for yourself on a .htaccess protected directory and post if you change the code that i used to do it

deathGod - 07 Sep 2008

if what you say is right, then souldn't the following give me my user name and password.it doesn't on my localhost.

<?php
if(isset($_SERVER['REMOTE_USER']))
print "username {$_SERVER['REMOTE_USER']}";
else print "error username<br>";

if(isset($_SERVER['REMOTE_PW']))
print "password {$_SERVER['REMOTE_PW']}";
else print "error password";
?>

anything i haven't done?

A PHP User - 07 Sep 2008

it's not accesssing mine.
Is User and Pass reserved? I don't see the dollar sign in front, that's why I ask.

Also, what kind of security can possibly be breached here? Seems like if it was that easy, then what's the point in even trying to secure the site?

My .htaccess uses username/password combo and the password file is on a different drive.

I am totally confused by this User/Pass thing you posted.
Maybe I shouldn't read the comments until I read the whole book.....

user - 07 Sep 2008

You can access usernames and passwords from authentication through .htacces using the $_SERVER superglobal.

If php is an Apache module then:
User = $_SERVER['PHP_AUTH_USER']
Pass = $_SERVER['PHP_AUTH_PW']

If php is not an Apache module you will have to use:
User = $_SERVER['REMOTE_USER']
Pass = $_SERVER['REMOTE_PW']



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is five plus nine?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow