Hi

I need help displaying certain server variables

below is my code

<?php
error_reporting(0); // turn error messages off
echo "<b>AUTH_TYPE</b>: When running under Apache as module doing HTTP authenticated, this variable holds the authentication type."; // prints definition of AUTH_TYPE
echo "<br><br>";
echo $_SERVER['AUTH_TYPE']; // prints the server variable AUTH_TYPE
echo "<br><br>";
echo "<b>DOCUMENT_ROOT</b>: The document root directory under which the script is executing, as defined in the server's configuration file."; // prints definition of DOCUMENT_ROOT
echo "<br><br>";
echo $_SERVER['DOCUMENT_ROOT']; // prints the server variable DOCUMENT_ROTT
echo "<br><br>";
echo "<b>GATEWAY_INTERFACE</b>: What revision of the CGI specification the server is using; such as 'CGI/1.1'."; // prints definition of GATEWAY_INTERFACE
echo "<br><br>";
echo $_SERVER['GATEWAY_INTERFACE']; // prints the server variable GATEWAY_INTERFACE
echo "<br><br>";
echo "<b>PATH_TRANSLATED</b>: File system-based path to the current script.";  // prints definition of PATH_TRANSLATED
echo "<br><br>";
echo $_SERVER['PATH_TRANSLATED']; // prints the server variable PATH_TRANSLATED
echo "<br><br>";
echo "<b>PHP_AUTH_PW</b>: When running under Apache as module doing HTTP authentication, this variable holds the password provided by the user.";  // prints definition of PHP_AUTH_PW
echo "<br><br>";
echo $_SERVER['PHP_AUTH_PW']; // prints the server variable PHP_AUTH_PW
echo "<br><br>";
echo "<b>PHP_AUTH_USER</b>: When running under Apache as module doing HTTP authentication, this variable holds the username provided by the user."; // prints definition of PHP_AUTH_USER
echo "<br><br>";
echo $_SERVER['PHP_AUTH_USER']; // prints the server variable PHP_AUTH_USER
echo "<br><br>";
echo "<b>PHP_SELF</b>: The filename of the currently executing script, relative to the document root."; // prints definition of PHP_SELF
echo "<br><br>";
echo $_SERVER['PHP_SELF']; // prints the server variable PHP_SELF
echo "<br><br>";
echo "<b>QUERY_STRING</b>: The query string, if there was any, with which the page was accessed."; // prints definition of QUERY_STRING
echo "<br><br>";
echo $_SERVER['QUERY_STRING']; // prints the server variable QUERY_STRING
echo "<br><br>";
echo "<b>REMOTE_ADDR</b>: The IP address from which the user is viewing the current page."; // prints definition of REMOTE_ADDR 
echo "<br><br>";
echo $_SERVER['REMOTE_ADDR']; // prints the server variable REMOTE_ADDR
echo "<br><br>";
echo "<b>REMOTE_HOST</b>: The Host name from which the user is viewing the current page."; // prints definition of REMOTE_HOST
echo "<br><br>";
echo $_SERVER['REMOTE_HOST']; // prints the server variable REMOTE_HOST
echo "<br><br>";
echo "<b>REMOTE_PORT</b>: The port being used on the user's machine to communicate with the Web server."; // prints definition of REMOTE_PORT
echo "<br><br>";
echo $_SERVER['REMOTE_PORT']; // prints the server variable REMOTE_PORT
echo "<br><br>";
echo "<b>REQUEST_METHOD</b>: Specifies which request method was used to access the page; such as 'GET', 'HEAD', 'POST', 'PUT'."; // prints definition of REQUEST_METHOD
echo "<br><br>";
echo $_SERVER['REQUEST_METHOD']; // prints the server variable REQUEST_METHOD
echo "<br><br>";
echo "<b>REQUEST_URI</b>: The URI which was given in order to access this page, such as '/index.html'."; // prints definition of REQUEST_URI
echo "<br><br>";
echo $_SERVER['REQUEST_URI']; // prints the server variable REQUEST_URI
echo "<br><br>";
echo "<b>SCRIPT_FILENAME</b>: The abosolute pathname of the currently executing script."; // prints definition of SCRIPT_FILENAME
echo "<br><br>";
echo $_SERVER['SCRIPT_FILENAME']; // prints the server variable SCRIPT_FILENAME
echo "<br><br>";
echo "<b>SCRIPT_NAME</b>: Contains the current script's path. This is useful for pages that need to point to themselves."; // prints definition of SCRIPT_NAME
echo "<br><br>";
echo $_SERVER['SCRIPT_NAME']; // prints the server variable SCRIPT_NAME
echo "<br><br>";
echo "<b>SERVER_ADMIN</b>: The value given to the SERVER_ADMIN (for apache) directive in the Web server configuration file."; // prints definition of SERVER_ADMIN
echo "<br><br>";
echo $_SERVER['SERVER_ADMIN']; // prints the server variable SERVER_ADMIN
echo "<br><br>";
echo "<b>SERVER_NAME</b>: The name of the server host under which the script is executing."; // prints definition of SERVER_NAME
echo "<br><br>";
echo $_SERVER['SERVER_NAME']; // prints the server variable SERVER_NAME
echo "<br><br>";
echo "<b>SERVER_PORT</b>: The port on the server machine being used by the web server for communication. By default setup, this is '80'."; // prints definition of SERVER_PORT
echo "<br><br>";
echo $_SERVER['SERVER_PORT']; // prints the server variable SERVER_PORT
echo "<br><br>";
echo "<b>SERVER_PROTOCOL</b>: Name and revision of the information protocol via which the page was requested; such as 'HTTP / 1.0'."; // prints definition of SERVER_PROTOCOL
echo "<br><br>";
echo $_SERVER['SERVER_PROTOCOL']; // prints the server variable SERVER_PROTOCOL
echo "<br><br>";
echo "<b>SERVER_SIGNATURE</b>: String containing ther server version and virtual host name, which are added to server-generated pages."; // prints definition of SERVER_SIGNATURE
echo "<br><br>";
echo $_SERVER['SERVER_SIGNATURE']; // prints the server variable SERVER_SIGNATURE
echo "<br><br>";
echo "<b>SERVER_SOFTWARE</b>: The server identification string."; // prints definition of SERVER_SOFTWARE
echo "<br><br>";
echo $_SERVER['SERVER_SOFTWARE']; // prints the server variable SERVER_SOFTWARE

?>

Below is the output:

AUTH_TYPE: When running under Apache as module doing HTTP authenticated, this variable holds the authentication type.

DOCUMENT_ROOT: The document root directory under which the script is executing, as defined in the server's configuration file.

C:/wamp/www/

GATEWAY_INTERFACE: What revision of the CGI specification the server is using; such as 'CGI/1.1'.

CGI/1.1

PATH_TRANSLATED: File system-based path to the current script.

PHP_AUTH_PW: When running under Apache as module doing HTTP authentication, this variable holds the password provided by the user.

PHP_AUTH_USER: When running under Apache as module doing HTTP authentication, this variable holds the username provided by the user.

PHP_SELF: The filename of the currently executing script, relative to the document root.

/server.php

QUERY_STRING: The query string, if there was any, with which the page was accessed.

REMOTE_ADDR: The IP address from which the user is viewing the current page.

127.0.0.1

REMOTE_HOST: The Host name from which the user is viewing the current page.

REMOTE_PORT: The port being used on the user's machine to communicate with the Web server.

51176

REQUEST_METHOD: Specifies which request method was used to access the page; such as 'GET', 'HEAD', 'POST', 'PUT'.

GET

REQUEST_URI: The URI which was given in order to access this page, such as '/index.html'.

/server.php

SCRIPT_FILENAME: The abosolute pathname of the currently executing script.

C:/wamp/www/server.php

SCRIPT_NAME: Contains the current script's path. This is useful for pages that need to point to themselves.

/server.php

SERVER_ADMIN: The value given to the SERVER_ADMIN (for apache) directive in the Web server configuration file.

admin@localhost

SERVER_NAME: The name of the server host under which the script is executing.

localhost

SERVER_PORT: The port on the server machine being used by the web server for communication. By default setup, this is '80'.

80

SERVER_PROTOCOL: Name and revision of the information protocol via which the page was requested; such as 'HTTP / 1.0'.

HTTP/1.1

SERVER_SIGNATURE: String containing ther server version and virtual host name, which are added to server-generated pages.

SERVER_SOFTWARE: The server identification string.

Apache/2.2.17 (Win32) PHP/5.3.5

As you can see its prints all the server variables except, AUTH_TYPE, PATH_TRANSLATED, PHP_AUTH_PW, PHP_AUTH_USER, QUERY_STRING, REMOTE_HOST, SERVER_SIGNATURE.

If I turn error reporting on, I get an undefined index error for these variables

I need help to display these variables

Any assitance will be appreciated

Thanks

    cybastud;11002373 wrote:

    I need help to display these variables

    You can't disable those variables... mainly because they don't exist (hence the undefined index error).

      You can do a print_r() or var_dump() of $_SERVER to find out what elements are available in your particular situation (server config and how you are calling the script).

        bradgrafelman;11002374 wrote:

        You can't disable those variables... mainly because they don't exist (hence the undefined index error).

        Disable? I do not want to disable anything, I want to know why I can print all the other server variable besides the ones listed above. Is there perhaps something I need to configure in Apache or something else I'm missing or do not understand about these server variables? To me my php looks fine...any suggestions?

          NogDog;11002375 wrote:

          You can do a print_r() or var_dump() of $_SERVER to find out what elements are available in your particular situation (server config and how you are calling the script).

          Sorry Im new to php, how exactly do I do this?

            cybastud;11002385 wrote:

            Disable? I do not want to disable anything, I want to know why I can print all the other server variable besides the ones listed above.

            Sorry... I meant display, not disable. Again, if you're getting an undefined index error, then the reason you can't display that particular item of the array is that it does not exist.

            cybastud;11002386 wrote:

            Sorry Im new to php, how exactly do I do this?

            You simply call [man]var_dump/man (or [man]print_r/man), passing in whichever variable you want to inspect. Also note that you might want to wrap the output in HTML <pre>..</pre> tags to preserve the whitespace in those functions' output (which makes it a lot easier to read).

              The long and short of it is that not every (any?) $SERVER variable is guaranteed to be set in all situations. An obvious example would be any of the $SERVER['HTTP_<something>'] elements, which would not be set if you ran your PHP script via the command line. Even with web requests, the particular web server configuration will affect it and the type of PHP installation (which SAPI, or maybe you're using CGI?).

              So, if you want to know which ones are currently available (and may be subject to change as your situation changes):

              <?php
              echo "<pre>";
              print_r($_SERVER);
              echo "</pre>";
              

              If you want to check before using it in a script:

              if(isset($_SERVER['foo'])) {
                echo $_SERVER['foo'];
              }
              else {
                echo "I don't know foo";
              }
              
                NogDog wrote:

                An obvious example would be any of the $SERVER['HTTP<something>'] elements, which would not be set if you ran your PHP script via the command line.

                As NogDog and their names imply, these particular indexes are only defined if you call the script via a web server.

                But not only are they dependent on server configuration, but also on the client you're using to connect to the server: http headers in the request sent by the client appear in the $SERVER array, (prefixed with 'HTTP' to prevent it colliding with any of the array's other elements).

                For example, I'm one of those who have a phpinfo.php file in the document root on my dev machine:

                <html><head><title>It's Alive!!!!!</title></head><body>
                <?php phpinfo(); ?>
                </body></html>

                for quick config checking. I hit [font=monospace]http://localhost/phpinfo.php[/font] just now and under PHP Variables is the line

                _SERVER["HTTP_DNT"]         1
                

                Corresponding to the DNT "Do Not Track" request header that Firefox includes when you have that option set.

                  NogDog;11002389 wrote:

                  The long and short of it is that not every (any?) $SERVER variable is guaranteed to be set in all situations. An obvious example would be any of the $SERVER['HTTP_<something>'] elements, which would not be set if you ran your PHP script via the command line. Even with web requests, the particular web server configuration will affect it and the type of PHP installation (which SAPI, or maybe you're using CGI?).

                  So, if you want to know which ones are currently available (and may be subject to change as your situation changes):

                  <?php
                  echo "<pre>";
                  print_r($_SERVER);
                  echo "</pre>";
                  

                  If you want to check before using it in a script:

                  if(isset($_SERVER['foo'])) {
                    echo $_SERVER['foo'];
                  }
                  else {
                    echo "I don't know foo";
                  }
                  

                  If I run this:

                  <?php
                  echo "<pre>";
                  print_r($_SERVER);
                  echo "</pre>"; 

                  then I get

                  Array
                  (
                  [HTTP_HOST] => localhost
                  [HTTP_USER_AGENT] => Mozilla/5.0 (Windows NT 6.1; rv:11.0) Gecko/20100101 Firefox/11.0
                  [HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
                  [HTTP_ACCEPT_LANGUAGE] => en-us,en;q=0.5
                  [HTTP_ACCEPT_ENCODING] => gzip, deflate
                  [HTTP_CONNECTION] => keep-alive
                  [HTTP_REFERER] => http://localhost/server.php
                  [HTTP_CACHE_CONTROL] => max-age=0
                  [PATH] => C:\oraclexe\app\oracle\product\10.2.0\server\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Microsoft SQL Server\90\Tools\binn\;%PROGRAMFILES%\Internet Explorer;C:\Program Files\Internet Explorer
                  [SystemRoot] => C:\Windows
                  [COMSPEC] => C:\Windows\system32\cmd.exe
                  [PATHEXT] => .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
                  [WINDIR] => C:\Windows
                  [SERVER_SIGNATURE] =>
                  [SERVER_SOFTWARE] => Apache/2.2.17 (Win32) PHP/5.3.5
                  [SERVER_NAME] => localhost
                  [SERVER_ADDR] => 127.0.0.1
                  [SERVER_PORT] => 80
                  [REMOTE_ADDR] => 127.0.0.1
                  [DOCUMENT_ROOT] => C:/wamp/www/
                  [SERVER_ADMIN] => admin@localhost
                  [SCRIPT_FILENAME] => C:/wamp/www/server.php
                  [REMOTE_PORT] => 50038
                  [GATEWAY_INTERFACE] => CGI/1.1
                  [SERVER_PROTOCOL] => HTTP/1.1
                  [REQUEST_METHOD] => GET
                  [QUERY_STRING] =>
                  [REQUEST_URI] => /server.php
                  [SCRIPT_NAME] => /server.php
                  [PHP_SELF] => /server.php
                  [REQUEST_TIME] => 1335008028
                  )

                  Which I am already able to print using my initial code, but how do I print out the variables AUTH_TYPE, PATH_TRANSLATED, PHP_AUTH_PW, PHP_AUTH_USER or how do I configure my server or client so that these variables are available to me, Im sorry I do not really understand what you are talking about.

                    If you're not using user authentication (where users get a 401 response that asks for a username and password, and a 403 if they fail) then the AUTH variables won't be there (because no user authentication was done). In the code you posted you even write

                    echo "<b>PHP_AUTH_PW</b>: When running under Apache as module doing HTTP authentication, this variable holds the password provided by the user.";

                    Notice it says: When running under Apache as module doing HTTP authentication.

                    Similarly, if your server isn't doing any URL-rewriting then the PATH_TRANSLATED element won't be in the array (because no path translation was done).

                    Conversely, your array has an entry called WINDIR. Do you think a server running on Unix will have one of those?

                    cybastud wrote:

                    Im sorry I do not really understand what you are talking about.

                    We're just repeating what the manual says about the [man]_SERVER[/man] array:

                    The entries in this array are created by the web server. There is no guarantee that every web server will provide any of these; servers may omit some, or provide others not listed here.

                    You don't see them because they're not there. What's confusing about that?

                      Thanks for all the help, I understand now

                        Write a Reply...