GET / POST / Cookie / Request / Server / Files / Session

PHP automatically creates global variables containing data from a variety of external sources. This feature can be turned off by turning off the register_globals setting. With register_globals you can access this data via a number of special associative arrays listed below.

Remember, all should be in capital letters $_GET ,  $_POST ,  $_SERVER  ,  $_FILES  ,  $_COOKIE  ,  $_SESSION  , $_REQUEST 

$_GET method

As mentioned in our previous chapters of Form Handling, $_GET method allows us to fetch the query parameter from URL like :-

URL : http://php.w3clan.com/?name=JohnDoe&Age=60&city=LasVegas

<?php

echo $_GET['name'];    // This will print JohnDoe

echo $_GET['age'];     // This will print 60

echo $_GET['city'];    // This will print LasVegas

?>

$_POST

<form action="index.php" method="POST">
    <input type="text" name="foo" value="bar">
</form>

<?php


if(isset($_POST['foo'])) {
    echo $_POST['foo'];      // foo is name of input box 'name="foo"' 
}


?>

$_COOKIE - to fetch cookie

<?php
    SetCookie('foo','bar');



if(isset($_COOKIE['foo'])) {
    
    echo $_COOKIE['foo'];
    
}
?>

$_REQUEST - QuickHand for    GET/POST/Cookie in same Order

<?php

    SetCookie('foo','bar');


if(isset($_REQUEST['foo'])) {

    echo $_REQUEST['foo'];     // cookie with name 'foo' is set.
}

?>

$_SERVER

Special variables set by your web server. You can get a list of what is set by running this code on your server:

<?php

foreach($_SERVER as $key => $val) {
    echo '$_SERVER['.$key.'] = '.$val ."<br>\n";
}

?>

Output

$_SERVER[UNIQUE_ID] = V6xgqtW9EdQAAGs5P@gAAAAD
$_SERVER[TOKEN] = "this is a magic token to retrieve user..."
$_SERVER[HTTP_HOST] = php.w3clan.com
$_SERVER[HTTP_CONNECTION] = keep-alive
$_SERVER[HTTP_UPGRADE_INSECURE_REQUESTS] = 1
$_SERVER[HTTP_USER_AGENT] = Mozilla/5.0 (X11; Linux x86_64) AppleWeb...
$_SERVER[HTTP_ACCEPT] = text/html,application/xhtml+xml,applicat...
$_SERVER[HTTP_DNT] = 1
$_SERVER[HTTP_REFERER] = http://php.w3clan.com/tutorial/...
$_SERVER[HTTP_ACCEPT_ENCODING] = gzip, deflate, sdch
$_SERVER[HTTP_ACCEPT_LANGUAGE] = en-US,en;q=0.8
$_SERVER[HTTP_COOKIE] = dims=1334_684; PHPSESSID=s0d68ctokeojju7...
$_SERVER[PATH] = /usr/local/bin:/usr/bin:/bin
$_SERVER[SERVER_SIGNATURE] = <address>Apache/2.2.22 (Debian) ...
$_SERVER[SERVER_SOFTWARE] = Apache/2.2.22 (Debian)
$_SERVER[SERVER_NAME] = php.w3clan.com
$_SERVER[SERVER_ADDR] = 104.27.183.191
$_SERVER[SERVER_PORT] = 80
$_SERVER[REMOTE_ADDR] = your_ip_address
$_SERVER[DOCUMENT_ROOT] = /local/web/sites/sub-domain/php/www
$_SERVER[SERVER_ADMIN] = noreply@w3clan.com
$_SERVER[SCRIPT_FILENAME] = local/web/sites/sub-domain/php/www/tutorial
$_SERVER[REMOTE_PORT] = 58720
$_SERVER[GATEWAY_INTERFACE] = CGI/1.1
$_SERVER[SERVER_PROTOCOL] = HTTP/1.1
$_SERVER[REQUEST_METHOD] = GET
$_SERVER[QUERY_STRING] = 
$_SERVER[REQUEST_URI] = /tutorial
$_SERVER[SCRIPT_NAME] = /tutorial
$_SERVER[PATH_INFO] = /71/register-global
$_SERVER[PATH_TRANSLATED] = local/web/sites/sub-domain/php/www/starting_...
$_SERVER[PHP_SELF] = /tutorial/41/register-global
$_SERVER[REQUEST_TIME_FLOAT] = 1470914730.471
$_SERVER[REQUEST_TIME] = 1470914730

$_FILES

Used for the RFC1867 file upload feature.

$_FILES['userfile']['name']
$_FILES['userfile']['type']
$_FILES['userfile']['size']
$_FILES['userfile']['tmp_name']

$_SESSION

<?php

session_start();

$_SESSION['username'] = 'Administrator';

echo $_SESSION['username'];


//Also for destroying session, simply call   session_destroy();

?>

Loading ...

Related Results :

  1. GET / POST / Cookie / Request / Server / Files / Session
  2. PHP Cookie
  3. PHP Sessions
Note :
  • Related Posts are generally User Blog posts.
  • or Other tutorials from other networks of w3clan.com.
  • Any registered user can create related posts based on search term tags.

About the Author