$_SERVER['SCRIPT_NAME'] vs $_SERVER['PHP_SELF']

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

$_SERVER['SCRIPT_NAME'] vs $_SERVER['PHP_SELF']

Post by JayBird »

Are they exactly the same?

My host has disabled SCRIPT_NAME, so will have to use PHP_SELF instead.

I'm worried that they are different, otherwise, why have them both?

Thanks

Mark
Farrier
Forum Newbie
Posts: 12
Joined: Thu Mar 25, 2004 10:39 am

Post by Farrier »

It would be most correct to say "they will occasionally have the same value".

If you are using the command line version (the 'CLI'), then PHP_SELF will be undefined, and SCRIPT_NAME will have the relative path as specified by the user.

If you are using Apache under windows, then there seem to be known bugs with both - not sure if that's current though, but for more info on that, do a google search for: SCRIPT_NAME PHP_SELF

Apache under Linux and some other servers will set apparently SCRIPT_NAME to be the same as PHP_SELF, but from the documentation, this is wrong.

PHP_SELF should be the absolute URL path, SCRIPT_NAME the absolute OS path.

Say you have a script at "/home/bob/public_html/myscript.php", and that's accessible as "http://www.example.com/~bob/myscript.php".

As I understand it fromthe doc, when calling it by that URL, SCRIPT_NAME should be "/home/bob/public_html/myscript.php" and PHP_SELF should be "/~bob/myscript.php". What they are on your system may or may not match this.

Here's the relevant doc: see if you agree with how I'm reading it.
http://uk2.php.net/reserved.variables
aleigh
Forum Commoner
Posts: 26
Joined: Thu Mar 25, 2004 11:06 am
Location: Midwestern United States
Contact:

Post by aleigh »

Farrier wrote: PHP_SELF should be the absolute URL path, SCRIPT_NAME the absolute OS path.

Say you have a script at "/home/bob/public_html/myscript.php", and that's accessible as "http://www.example.com/~bob/myscript.php".

As I understand it fromthe doc, when calling it by that URL, SCRIPT_NAME should be "/home/bob/public_html/myscript.php" and PHP_SELF should be "/~bob/myscript.php". What they are on your system may or may not match this.
You sort of have to be careful because it all goes to hell when user directories are involved. Technically there is no such thing as a ~ directory as far as the server is concerned; as web server providers we just happen to implement a magical URL re-write that acomplishes just that.

This can cause problems for innocent PHP scripts that don't keep this in mind, lots of problems.

The classic example is really a website that has a document root, for example, /web/pages

If you request http://yoursite/foo/bar.php and the document root is /web/pages, then,

PHP_SELF should be /foo/bar.php
SCRIPT_FILENAME should be /web/pages/foo/bar.php

It gets fuzzy for a user URLs, because, what is the document root?

Common sense would tell us that the document is /home/username/public_html (or whatever) and if that's the case then PHP_SELF shouldn't be /~user/foo.php because PHP_SELF is in relation to the document root! It would be /foo.php.

This behavior isn't really very well defined (that I know of) so there is some slop in the different implementations. I know I wasn't quite sure what to do.
Post Reply