Page 1 of 1

what is the difference ($_SERVER)

Posted: Thu Sep 09, 2004 8:12 am
by newmember
I'm a little confused with this uses of this variables:

'PHP_SELF'
'DOCUMENT_ROOT'
'SCRIPT_FILENAME'
'PATH_TRANSLATED'
'SCRIPT_NAME'

in what situations do i need them?

thanks

Posted: Thu Sep 09, 2004 8:32 am
by MarK (CZ)
Sometimes it's useful to know what script are you running etc. If you use them well, it can make your scripts more portable (to another server or so).

Posted: Thu Sep 09, 2004 8:55 am
by newmember
ok
can you give a real-life example of use?

Posted: Thu Sep 09, 2004 9:45 am
by MarK (CZ)

Code: Select all

<?php

if (!Empty($_POST['input_field'])) {
  
  // handle the variable
  // if not empty
  
} else {
  // ask for the variable otherwise
?>
<form method="post" action="<?= $_SERVER['PHP_SELF'] ?>">
 <input type="text" name="input_field"></input>
 <button type="submit">Submit form</button>
</form>
<?php
}
?>
Advantage: You can rename the script as needed (the form will always refer to itself).

Posted: Thu Sep 09, 2004 9:48 am
by newmember
ok thanks
(saw this example in so many places:))

Posted: Thu Sep 09, 2004 11:31 am
by MarK (CZ)
newmember wrote:ok thanks
(saw this example in so many places:))
The same one?
I wrote it myself, no copying :?

Posted: Thu Sep 09, 2004 12:10 pm
by newmember
i mean the idea.. not the exact code:)

Posted: Thu Sep 09, 2004 12:13 pm
by newmember
by the way, what = means?

"<?= $_SERVER['PHP_SELF'] ?>"

i never saw this in manual...

Posted: Thu Sep 09, 2004 12:54 pm
by feyd
<?=

<?php echo

Posted: Thu Sep 09, 2004 1:18 pm
by MarK (CZ)
You have to have some option turned On in php.ini to use this (i guess it's 'short_open_tag').

Posted: Thu Sep 09, 2004 11:44 pm
by Breckenridge
Assume that your script aka php file named myfile.php is located under: /var/www/public/


'PHP_SELF' is the script file name:
myfile.php
common use:
<form action='<? echo $_SERVER['PHP_SELF']?>'>


'DOCUMENT_ROOT' is the root dir of your web server /var/www/public/
common use:
<?
include $_SERVER['DOCUMENT_ROOT']."/lib/common.php";
?>


'SCRIPT_FILENAME' = PHP_SELF without path

'PATH_TRANSLATED' path to script i.e. / or /images/

'SCRIPT_NAME' file name + any $_GET variables I think. I will verify and fix this post if needed.