THE SUPERGLOBAL ARRAY $_SERVER
A very important superglobal array is $_SERVER. It contains information about the request, the server itself, headers, paths, and the script. This is the information extracted via a var_dump($_SERVER).
We have information about the script, SCRIPT_NAME, about the QUERY_STRING in this case empty, we have the method of the request REQUEST_METHOD in our case I did a GET through HTTP/1.1. There is the full path to the PHP script SCRIPT_FILENAME, the communication SERVER_PORT happened on port 80 as the default HTTP protocol uses this port, if we use HTTPS the port is 443. The information on the SERVER my system is using Apache on Windows 64 bit, and the headers regarding the request made by the client, for example the client sends language preferences, in my case HTTP_ACCEPT_LANGUAGE is set to en-IT as the first preference, then en_US and finally en. q is the Quality-Factor, the higher this value the greater the preference.
ENCODING
We have encoding information, basically we are telling the Server if you want to compress the content with these algorithms you can do it since as a client they are able to decompress it. This results in less bandwidth as we send less data and higher speed. Then we have HTTP_CONNECTION basically that value is given by the client to make sure that it doesn’t close the TCP connection on every request. Finally, we have the request time as a float and as an integer.
THE SUPERGLOBAL ARRAY $_FILES
This Array gives us information about a file loaded with an HTML template having a form with POST method and enctype set to multipart/form-data. The following code shows such an Array.
FURTHER INFORMATION
In PHP, the superglobals $SERVER and $FILES are associative arrays that provide useful information within a script.
Here is a detailed explanation of both in Italian:
The Super Global Array $SERVER
The $SERVER superglobal array contains information about the server environment and the PHP script execution context.
This array is populated by variables set by the web server or directly by the PHP script itself.
Some of the most common elements found in $SERVER are:
-$_SERVER[‘PHP_SELF’]: Returns the path to the currently running PHP file, starting at the document root.
-$_SERVER[‘SERVER_NAME’]: Contains the host name of the web server where the script is running, e.g. “www.example.com”.
-$_SERVER[‘HTTP_HOST’]: Contains the value of the HTTP “Host” header sent by the client.
-$_SERVER[‘REQUEST_METHOD’]: Indicates the HTTP method used to access the page (e.g., “GET”, “POST”).
-$_SERVER[‘QUERY_STRING’]: Contains the query string, that is, the part of the URL that follows the question mark (?).
-$_SERVER[‘HTTP_USER_AGENT’]: Returns the client’s user agent, that is, information about the browser and operating system used by the user.
-$_SERVER[‘REMOTE_ADDR’]: Returns the IP address of the client that requested the script.
Example of use:
echo $_SERVER[‘SERVER_NAME‘]; // Display the name of the server
echo $_SERVER[‘REQUEST_METHOD‘]; // Display the HTTP method used
The Super Global Array $FILES
The $FILES superglobal array is used to manage files uploaded via an HTML form.
It is populated only when a file is submitted through a form with the POST method, using the enctype=”multipart/form-data” attribute .
The $_FILES array is structured as a multi-dimensional associative array, where each loaded file is represented by a sub-array containing the following elements:
-$_FILES[‘field_name’][‘name’]: The original file name on the client computer.
-$_FILES[‘field_name’][‘type’]: The MIME type of the file, for example “image/jpeg”.
-$_FILES[‘field_name’][‘tmp_name’]: The temporary path on the server where the uploaded file was saved.
-$_FILES[‘field_name’][‘error’]: An error code associated with loading the file.
-$_FILES[‘field_name’][‘size’]: The size of the file in bytes.
Example of use:
if ($_FILES[‘fileToUpload’][‘error’] == UPLOAD_ERR_OK) {
$tmp_name = $_FILES[‘fileToUpload’][‘tmp_name’];
$name = basename($_FILES[‘fileToUpload’][‘name’]);
move_uploaded_file($tmp_name, “uploads/$name”);
echo “File caricato con successo.”;
} else {
echo “Errore durante il caricamento del file.”;
}
In this example, the file is moved from the temporary directory to a directory called “uploads” on the server.
Conclusion
– $SERVER is mainly used to collect information about the environment and the HTTP request .
– $FILES is specifically for handling files uploaded via HTML forms.
These superglobal arrays are fundamental tools in PHP for handling requests and interaction with the server.
Leave A Comment