PHP predefined variables - Superglobals

several predefined variables in php are 'superglobals', which means they are available in all scopes throughout a script. and you can access them from any function, class or file without having to do anything special.

the php superglobal variables are :

  • $globals : references all variables available in global scope
  • $_server : server and execution environment information
  • $_get : http get variables
  • $_Post : http post variables
  • $_files : http file upload variables
  • $_cookie : http cookies
  • $_session : session variables
  • $_request : http request variables
  • $_env : environment variables

PHP $global

$global is an associative array containing references to all variables which are currently defined in the global scope of the script.

the all global variables are accessed using an array syntax: $globals[index].

   <?php
              $str = "i like apple";
              
              $a   = "what you want";
              
              function  show(){
              
                                       echo  $globals['str'];
                              
                                       echo  "<br/>". $globals['a'];

                                      }
                                      
                               show();  
   ?>

PHP $_GET

PHP $_get variable can also be used to get form information after submitting an html form with method="get".

PHP $_get can also collect information sent in the url.for example in this link

   <a href="http://example.com/profile.php?uid=232323&video=12>example.com</a>

if a user click on the 'example.com' the parameter 'uid' and 'video' is send to 'profile.php', and you can access their values in "profile.php" using $_get.

       <body>
       
          <?php
          
                    echo  "uid value ". $_get['uid']." video value ". $_get['video'];
                         
          ?>
          
     </body>

PHP $_POST

forms submitted using method='post' send their values in the body of the http request, posted data is available in your script by using $_post.

                            // file 'data.html'
               <body>
               
                   <form  action="login.php"  method="post">
                   
                   <username : <input type="text"   name="uname"><br/>
                   
                   <password: <input type="text"  name="pass"><br/>
                   
                   <input type ="submit"  value="login">
                   
              </body>
              
                            //login.php file
                            
                          <?php                            
                                     echo "username = ". $_post['uname'];
                                                   
                                     echo "password = ". $_post['pass'];
                                                                    
                          ?>

PHP $_REQUEST

the $_request superglobal is an amalgam of $_post and $_get data.it's offered as a convenience, but as a general rule i advice people to avoid it.it create some security risk.


PHP $_SERVER

$_server is an array containing information such as headers, paths, and script locations.

$_server array references dependent on the web server because its created by the web server.

syntax:

         $_server['value'];
    <?php
    
            echo   "<br/>". $_server['server_name'];
            
            echo   "<br/>". $_server['server_protocol'];
            
            echo   "<br/>". $_server['server_port'];
            
            echo   "<br/>". $_server['script_name'];
            
            echo   "<br/>". $_server['request_method'];
            
            echo   "<br/>". $_server['http_host'];
     ?>
php codedescription
$_server['php_self]return the filename of the currently executing script.
$_server['gateway_interface']return the version of the cgi the server is using
$_server['server_addr']return the ip address of the host server.
$_server['server_name']return the name of the host server
$_server['server_software']return the server identification string, apache/2.2.25
$_server['server_protocol']name and revision of the information protocol.(e.g http/1.1)
$_server['request_method']return the request method used to access the page
$_server['request_time']return the timestamp of the start of the request
$_server['query_string']return the query string if the page is accessed via a query string
$_server['document_root']return the document root directory.
$_server['http_accept']return the header from the current request.
$_server['http_accept_charset']return the accept-charset header from the current request (e.g 'utf-8')
$_server['http_accept_encoding']return the accept-encoding header from the current request('gzip')
$_server['http_accept_language']return the accept-language header from the current request (e.g 'en')
$_server['http_connection']return the connection header from the current request.(e.g 'keep-alive')
$_server['http_host']return the host header from the current request
$_server['http_referer']return the complete url of the current page
$_server['https']is the script queried through a secure http protocol
$_server['remote_addr']return the ip address from which the user is viewing the current page
$_server['remote_host']return the host name from which the user is viewing the current page
$_server['remote_port']port being used to the user's machine to communicate with the web server
$_server['remote_user']the authenticated user
$_server['script_filename']return the absolute pathname of the currently executing script
$_server['server_admin'] 
$_server['server_port']the port on the server machine being used by web server for communication.
$_server['server_signature']the server version and virtual host name which are added to server-generated pages.
$_server['path_translated']returns the file system based path to the current script
$_server['script_name']returns the path of the current script
$_server['script_uri']returns the uri of the current page