PHP Predefined variables
sample URL like and after submit
http://localhost/test.php --> http://localhost/result.php?id=21
Server variables: $_SERVER : Server and execution environment information.
$_SERVER['PHP_SELF'] output : /result.php
$_SERVER['GATEWAY_INTERFACE'] output :CGI/1.1
$_SERVER['SERVER_ADDR'] output : 127.0.0.1
$_SERVER['REMOTE_ADDR'] output : 127.0.0.1
$_SERVER['SERVER_NAME'] output : localhost
$_SERVER['HTTP_HOST'] output : localhost
$_SERVER['SERVER_SOFTWARE'] output : Apache/2.2.17 (Win32) PHP/5.3.4
$_SERVER['HTTP_CONNECTION'] output : keep-alive
$_SERVER['SERVER_PROTOCOL'] output : HTTP/1.1
$_SERVER['SERVER_ADMIN'] output : admin@localhost
$_SERVER['REQUEST_METHOD'] output : POST/ GET
$_SERVER['SERVER_PORT '] output : 80
$_SERVER['REQUEST_TIME'] output :1420187992
$_SERVER['QUERY_STRING'] output : id=21
$_SERVER['REQUEST_URI'] output : /result.php?id=21
$_SERVER['SCRIPT_NAME'] output :/result.php
$_SERVER['SCRIPT_FILENAME'] output : C:/wamp/www/result.php
$_SERVER['SCRIPT_NAME'] output :/result.php
$_SERVER['DOCUMENT_ROOT'] output : C:/wamp/www/
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
$_SERVER['HTTP_REFERER'] output : http://localhost/test.php (previous page)
$_SERVER['DOCUMENT_ROOT'] output : C:/wamp/www/
$GLOBALS : Global variables References all variables available in global scope.
Example:
$x = 75; $y = 25;
function addition() {
$GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y'];
}
addition();
echo $z;
Output:
100
$_GET: HTTP GET variables.
URL like and after submit
http://localhost/test.php --> http://localhost/result.php?id=21
$_GET['id']
Output : 21
$_GET['id']
Output : 21
$_POST: HTTP POST variables.
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Name: <input type="text" name="fname">
<input type="submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect value of input field
$name = $_POST['fname'];
if (empty($name)) {
echo "Name is empty";
} else {
echo $name;
}
}
?>
$_FILES: HTTP File Upload variables.
An associative array of items uploaded to the current script via the HTTP POST method
$_REQUEST: HTTP Request variables.
Example:
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Name: <input type="text" name="fname">
<input type="submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect value of input field
$name = $_REQUEST['fname'];
if (empty($name)) {
echo "Name is empty";
} else {
echo $name;
}
}
?>
$_SESSION: Session variables.
A session creates a file in a temporary directory on the server where registered session variables and their values are stored. This data will be available to all pages on the site during that visit
Example:
<?php
session_start();
if( isset( $_SESSION['counter'] ) )
{
$_SESSION['counter'] += 1;
}
else
{
$_SESSION['counter'] = 1;
}
$msg = "You have visited this page ". $_SESSION['counter'];
$msg .= "in this session.";
?>
<html>
<head>
<title>Setting up a PHP session</title>
</head>
<body>
<?php echo ( $msg ); ?>
</body>
</html>
Output
You have visited this page 1in this session.
(after refresh )
You have visited this page 2in this session.......
$_COOKIE: HTTP Cookies.
Cookies are text files stored on the client computer and they are kept of use tracking purpose. PHP transparently supports HTTP cookies.
setcookie(name, value, expire, path, domain, security);
Example:
setcookie("name", "John Watkin", time()+3600, "/","", 0);
setcookie("age", "36", time()+3600, "/", "", 0);
Output
Set Cookies John Watkin
36
Good....Keep it up...
ReplyDeleteThanks for u support...
Delete