Page 1 of 1

$_SERVER['PHP_SELF'] -> usefulness?

Posted: Wed Nov 22, 2006 11:41 am
by konstandinos
tutorials insist on using this when assigning the action attribute to a form tag, ie:

Code: Select all

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" etc.
from what i understand, all this does is return the url from the document root up. ie: using the code on http://www.kosta.com/foo/login.php would just give /foo/login.php

but now i ask, what's the point?

using the code below works perfectly fine:

Code: Select all

<form action="login.php"
all i want is to call the same file that the script is in so why all the fancy superglobal code?

i hope this isn't a silly question. my gut thinks it is.

Posted: Wed Nov 22, 2006 12:11 pm
by Burrito
actually if you want to return the page to itself, you don't even need the action attribute of the tag. Alternatively (and best practice) you can use #.

Posted: Wed Nov 22, 2006 2:16 pm
by RobertGonzalez
A little safer way (than using $_SERVER['PHP_SELF']) is to use basename($_SERVER['SCRIPT_FILENAME']).

Reasoning for using superglobal page name references varies, but typically it has to do with portability. Generally speaking, if you have a reference to the scripts page name throughout the page, you can set it in the form of a var and use that var throughout the page. Then, if the page name changes, the var automagically changes with it, rather than you having to re-hardcode the page name throughout the script.