what is the difference ($_SERVER)

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
newmember
Forum Contributor
Posts: 252
Joined: Fri Apr 02, 2004 12:36 pm

what is the difference ($_SERVER)

Post 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
User avatar
MarK (CZ)
Forum Contributor
Posts: 239
Joined: Tue Apr 13, 2004 12:51 am
Location: Prague (CZ) / Vienna (A)
Contact:

Post 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).
User avatar
newmember
Forum Contributor
Posts: 252
Joined: Fri Apr 02, 2004 12:36 pm

Post by newmember »

ok
can you give a real-life example of use?
User avatar
MarK (CZ)
Forum Contributor
Posts: 239
Joined: Tue Apr 13, 2004 12:51 am
Location: Prague (CZ) / Vienna (A)
Contact:

Post 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).
User avatar
newmember
Forum Contributor
Posts: 252
Joined: Fri Apr 02, 2004 12:36 pm

Post by newmember »

ok thanks
(saw this example in so many places:))
User avatar
MarK (CZ)
Forum Contributor
Posts: 239
Joined: Tue Apr 13, 2004 12:51 am
Location: Prague (CZ) / Vienna (A)
Contact:

Post by MarK (CZ) »

newmember wrote:ok thanks
(saw this example in so many places:))
The same one?
I wrote it myself, no copying :?
User avatar
newmember
Forum Contributor
Posts: 252
Joined: Fri Apr 02, 2004 12:36 pm

Post by newmember »

i mean the idea.. not the exact code:)
User avatar
newmember
Forum Contributor
Posts: 252
Joined: Fri Apr 02, 2004 12:36 pm

Post by newmember »

by the way, what = means?

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

i never saw this in manual...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

<?=

<?php echo
User avatar
MarK (CZ)
Forum Contributor
Posts: 239
Joined: Tue Apr 13, 2004 12:51 am
Location: Prague (CZ) / Vienna (A)
Contact:

Post by MarK (CZ) »

You have to have some option turned On in php.ini to use this (i guess it's 'short_open_tag').
Breckenridge
Forum Commoner
Posts: 62
Joined: Thu Sep 09, 2004 11:10 pm
Location: Breckenridge, Colorado

Post 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.
Post Reply