Developer Snippet Diary

superglobals in php with explain with output

 

Some predefined variables in PHP are "superglobals", which means that they are always accessible, regardless of scope - and you can access them from any function, class or file without having to do anything special.

$x=10;

echo $_GLOBALS['x']; // access global variables because all variables are global

  • $_SERVER: Contains information about the server and environment, such as the URL, HTTP headers, and script location.
  • $_GET: Contains variables passed to the current script via the URL parameters.
  • $_POST: Contains variables submitted via an HTML form using the HTTP POST method.
  • $_FILES: Contains information about files uploaded via an HTML form using the HTTP POST method.
  • $_COOKIE: Contains variables stored in the user's browser cookies.
  • $_SESSION: Contains variables that are specific to the current user's session.
  • [OPENSSL_CONF] => C:/xampp/apache/bin/openssl.cnf
  • [TMP] => \xampp\tmp  
  • [HTTP_HOST] => localhost
  • [HTTP_CONNECTION] => keep-alive
  • [HTTP_USER_AGENT] => Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36 
  • [HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,application/signed-exchange;v=b3

  • [HTTP_COOKIE] => _ga=GA1.1.1951352871.1571924619;
  • [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_NAME] => localhost   
  • [SERVER_ADDR] => ::1
  • [SERVER_PORT] => 80
  • [REMOTE_ADDR] => ::1
  • [DOCUMENT_ROOT] => C:/xampp/htdocs
  • [REQUEST_SCHEME] => http
  • [SCRIPT_FILENAME] => C:/xampp/htdocs/test.php
  • [REMOTE_PORT] => 62305
  • [SERVER_PROTOCOL] => HTTP/1.1
  • [REQUEST_METHOD] => GET
  • [QUERY_STRING] =>
  • [REQUEST_URI] => /test.php
  • [SCRIPT_NAME] => /test.php
  • [PHP_SELF] => /test.php
  • $_REQUEST['nameOfFormField'];   // get form data
  • $_POST['fieldName'];// if form method is post
  • $_GET['fieldName'];// get form data if method is get
  • $_FILES //handle files upload etc
  • print_r($_COOKIE); //cookies
  • $_SESSION //sessions
Posted by: R GONDAL
Email: rizikmw@gmail.com