Several 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.
The PHP superglobal variables are:
This chapter will explain some of the superglobals, and the rest will be explained in later chapters.
$GLOBALS is a PHP superglobal variable that is used to access global variables from anywhere in the PHP script (also from within functions or methods).
PHP stores all global variables in an array called $GLOBALS["your_index"]. The index holds the name of the variable.
The example below shows how to use the super global variable $GLOBALS:
<?php
$x = 60;
$y = 40;
function addition() {
$GLOBALS["z"] = $GLOBALS["x"] + $GLOBALS["y"];
}
addition();
echo $z;
?>
Output : 100
<?php
echo $_SERVER["PHP_SELF"];
echo "<br>";
echo $_SERVER["SERVER_NAME"];
echo "<br>";
echo $_SERVER["HTTP_HOST"];
echo "<br>";
echo $_SERVER["HTTP_REFERER"];
echo "<br>";
echo $_SERVER["HTTP_USER_AGENT"];
echo "<br>";
echo $_SERVER["SCRIPT_NAME"];
?>
$_SERVER is an array also known as PHP superglobal variable that holds information about headers, paths and script location and entries in these arrays are created by web servers
Code | Description |
---|---|
$_SERVER['PHP_SELF'] | It is used to display the filename of the currently executing script |
$_SERVER['GATEWAY_INTERFACE'] | It is used to display the version of the Common Gateway Interface (CGI) the server is using |
$_SERVER['SERVER_ADDR'] | It is used to display the IP address of the host server |
$_SERVER['SERVER_NAME'] | It is used to display the host server's name |
$_SERVER['SERVER_SOFTWARE'] | It is used to display the server identification string. |
$_SERVER['SERVER_PROTOCOL'] | It is used to display the name and revision of the information protocol |
$_SERVER['REQUEST_METHOD'] | It is used to display the request method used to access the page. i.e. 'GET','POST' |
$_SERVER['REQUEST_TIME'] | It is used to display the timestamp of the start of the request. |
$_SERVER['QUERY_STRING'] | It is used to display the query string if the page is accessed via a query string |
$_SERVER['HTTP_ACCEPT'] | It is used to display the Accept header from the current request |
$_SERVER['HTTP_ACCEPT_CHARSET'] | It is used to display the Accept_Charset header from the current request |
$_SERVER['HTTP_HOST'] | It is used to display the Host header from the current request |
$_SERVER['HTTP_REFERER'] | It is used to display the complete URL of the current page (not reliable because not all user-agents support it) |
$_SERVER['HTTPS'] | Is the script queried through a secure HTTP protocol |
$_SERVER['REMOTE_PORT'] | It is used to display the port being used on the user's machine to communicate with the web server |
$_SERVER['SCRIPT_FILENAME'] | It is used to display the absolute pathname of the currently executing script |
$_SERVER['SERVER_ADMIN'] | It is used to display the value given to the SERVER_ADMIN directive in the web server configuration file. |
$_SERVER['REMOTE_ADDR'] | It is used to display the IP address from where the user is viewing the current page |
$_SERVER['REMOTE_HOST'] | It is used to display the Host name from where the user is viewing the current page |
$_SERVER['SERVER_PORT'] | It is used to display the port which is being used on the local machine to communicate with the web server. |
$_SERVER['SERVER_SIGNATURE'] | It is used to display the server version and virtual host name which are added to server-generated pages |
$_SERVER['PATH_TRANSLATED'] | It is used to display the file system based path to the current script |
$_SERVER['SCRIPT_NAME'] | It is used to display the current script's path |
$_SERVER['SCRIPT_URI'] | It is used to display the current page's URI |
<html>
<body>
<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 "My Name is Php";
} else {
echo $name;
}
}
?>
</body>
</html>
Here is simple example where i have submit form which is point to itself for process with data with GET method. Now in PHP script i am using the super global variable $_REQUEST to get the value of the input field.
PHP $_POST is widely used to collect form data after submitting an HTML form with method="post". $_POST is also widely used to pass variables.
<html>
<body>
<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 "My Name is Php";
} else {
echo $name;
}
}
?>
</body>
</html>
<html>
<body>
<?php
echo "Study " . $_GET["php"] . " at " . $_GET["development"];
?>
</body>
</html>
It can be used to upload files from a client computer/system to a server.
OR
Such as File Name, File Type, File Size, File temporary name.
A session variable is used to store information about a single user, and are available to all pages within one application.
A cookie is used to identify a user. cookie is a small file that the server embedded on user computer.
© 2024 Easy To Learning. All Rights Reserved | Design by Easy To Learning