Published Book on Amazon
All of IOT Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 1 | |
All of IOT Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 2 |
출판된 한글판 도서
최신 라즈베리파이(Raspberry Pi)로 시작하는 사물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (상) | |
최신 라즈베리파이(Raspberry Pi)로 시작하는 사물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (하) |
Original Book Contents
24.4.10 Transmission Methods of PHP and Key Global Variable
24.4.10.1 POST/GET Method of HTTP and PHP Processing
GET method and POST method can be used when a web browser calls the web server and send the necessary data.
● GET method
GET method constructs the transmitted data in URL format and sends the request in the following format.
http://URL/?key1=value1&key2=value2 |
Use "?" symbol to separate between URL and transmission data. The transmission data is specified in the format "key=value", and if there are multiple data, they are separated by "&" symbol.
In this method, the entire contents to be transmitted are displayed in URL format and displayed in the web browser. In addition, GET method has a constraint on the amount of data to be transmitted, but the processing speed is relatively faster than POST method.
This method is convenient to store the content of the URL as it is in "Favorites" etc. and to use it again later, but it is difficult to use when sending data requiring confidentiality. Therefore, it is recommended to use GET method when transmitting small volume and less sensitive data.
The data transmitted like this are stored in "$_GET" array variable and "$_REQUEST" array variable on the web server. The values stored here are used as follows.
$variable = $_GET["key"] |
$variable = $_REQUEST["key"] |
[Used Example]
The following shows GET method is specified in "form" tag of an HTML document.
<form action="employee.php" method="get"> Name: <input type="text" name="username" /><br /> Email: <input type="text" name="email" /><br /> <input type="submit" name="submit" value="Submit me!" /> </form> |
The following shows that the transmitted data is refered using variables "$_GET" and "$_REQUEST" in "employee.php" file called above.
<?php echo $_GET['username']; echo $_REQUEST['username']; ?> |
● POST method
POST method does not send the transmitted data as part of the URL. When using the POST method, the web client calls the web server, then internally calls POST function, and sends the transmission data as an argument of the function directly.
POST method has no limit on the amount of data to be transmitted, so it can be freely used even where a lot of data processing is required. If you use this method, the transmitted data is not displayed in a URL format in your web browser, so you can use it without problems even when you need to send confidential data.
The data transmitted like this is stored in "$_POST" array variable and "$ _REQUEST" array variable on the web server. The values stored here are used as follows.
$variable = $_POST["key"] |
$variable = $_REQUEST["key"] |
The following shows that POST method is specified in "form" tag of an HTML document.
<form action="employee.php" method="post"> Name: <input type="text" name="username" /><br /> Email: <input type="text" name="email" /><br /> <input type="submit" name="submit" value="Submit me!" /> </form> |
The following shows that the transmitted data is refered using "$_POST" variable and "$_REQUEST" variable in "employee.php" file called above.
<?php echo $_POST['username']; echo $_REQUEST['username']; ?> |
24.4.10.2 Global Variable of PHP
● $_SERVER
"$_SERVER" variable is an array data generated by the web server and contains information such as header, path, script location, and so on. There is no guarantee that all web servers will provide information about all items in the same format, and the items provided may vary according to the web server.
The major informations provided here are as follows.
Element/Code | Description |
PHP_SELF | Returns the filename of the currently executing script |
GATEWAY_INTERFACE | Returns the version of the Common Gateway Interface (CGI) the server is using |
SERVER_ADDR | Returns the IP address of the host server |
SERVER_NAME | Returns the name of the host server (such as www.w3schools.com) |
SERVER_PROTOCOL | Returns the name and revision of the information protocol (such as HTTP/1.1) |
REQUEST_METHOD | Returns the request method used to access the page (such as POST) |
REQUEST_TIME | Returns the timestamp of the start of the request (such as 1377687496) |
QUERY_STRING | Returns the query string if the page is accessed via a query string |
HTTP_HOST | Returns the Host header from the current request |
HTTPS | Is the script queried through a secure HTTP protocol |
REMOTE_ADDR | Returns the IP address from where the user is viewing the current page |
REMOTE_HOST | Returns the Host name from where the user is viewing the current page |
REMOTE_PORT | Returns the port being used on the user's machine to communicate with the web server |
SCRIPT_FILENAME | Returns the absolute pathname of the currently executing script |
SERVER_ADMIN | Returns the value given to the SERVER_ADMIN directive in the web server configuration file (if your script runs on a virtual host, it will be the value defined for that virtual host) (such as someone@w3schools.com) |
SERVER_PORT | Returns the port on the server machine being used by the web server for communication (such as 80) |
SCRIPT_NAME | Returns the path of the current script |
SCRIPT_URI | Returns the URI of the current page |
DOCUMENT_ROOT | The document root directory under which the current script is executing, as defined in the server's configuration file. |
Below is an example of using "$_SERVER" variable in a script.
<html> |
This use "$_SERVER['PHP_SELF']" to determine its own file name and assign it to "action" attribute. It use "$_SERVER["REQUEST_METHOD"]" to determine what the current HTTP request method is, and perform necessary processings only when the method is POST method.
● $GLOBALS
"$GLOBALS" is a PHP super global variable that stores all global variables in "$GLOBALS[index]" array. This variable allows you to use global variables anywhere in the PHP script, in a function, or within method of a class. Variable names are assigned to index.
It can be used in the following format to use that variable.
$variable = $GLOBALS[‘variable-name’] |
The following shows how to use the super global variable "$GLOBALS".
<?php |