Access server insights with PHP’s built-in predefined variables
You’ve seen them already if you’ve been following along. There are a few of them in PHP and we’ll cover some of the most common ones. In the previous article, we looked at the $_GET
and $_POST
array variables. PHP automatically has access to form submission variables since they’re added to the $_POST
variable. Let’s dive into the most common variables.
https://blog.devgenius.io/php-p75-get-and-post-request-methods-dafdb0a86562
$_SERVER
The $_SERVER
variable contains some common information that may be needed throughout your application. The array contains information like headers, paths, and script locations. There are predefined keys that you can call and it contains information about your application environment.
PHP_SELF
: The key contains the filename of the current script. For example, if you were to visit https://dinocajic.com/pages/dino-page.php, the call to $_SERVER['PHP_SELF']
would yield pages/dino-page.php
.
SERVER_ADDR
: Returns the IP address of the server. For example, 172.15.0.2.
REQUEST_METHOD
: Shows which method was used to access the page. i.e. get
, post
, put
, head
.
DOCUMENT_ROOT
: The document root under which the current script is being executed. For example, /var/www/html
.
HTTP_HOST
: Shows the host. i.e. 127.0.0.1
HTTP_REFERER
: Shows which page referred the user to this page. For example, if the user was on dinocajic.com/about
and they clicked on a link that directed them to dinocajic.com/contact
, the referrer would be the about
page.
REMOTE_ADDR
: Shows the IP address of the user viewing the page.
There are quite a few options available. For the full list, visit the PHP documentation. You can also explore by just var_dumping the variable.
var_dump($_SERVER);
array (size=32)
'HTTP_HOST' => string '0.0.0.0' (length=7)
'HTTP_CONNECTION' => string 'keep-alive' (length=10)
'HTTP_CACHE_CONTROL' => string 'max-age=0' (length=9)
'HTTP_DNT' => string '1' (length=1)
'HTTP_UPGRADE_INSECURE_REQUESTS' => string '1' (length=1)
'HTTP_USER_AGENT' => string 'Mozilla/5.0...' (length=117)
'HTTP_ACCEPT' => string 'text/html,application/...' (length=135)
'HTTP_ACCEPT_ENCODING' => string 'gzip, deflate' (length=13)
'HTTP_ACCEPT_LANGUAGE' => string 'en-US,en;q=0.9' (length=14)
'PATH' => string '/usr/local/sbin:/usr/local/bin...' (length=60)
'SERVER_SIGNATURE' => string '<address>Apache/2.4...' (length=68)
'SERVER_SOFTWARE' => string 'Apache/2.4.41 (Ubuntu)' (length=22)
'SERVER_NAME' => string '0.0.0.0' (length=7)
'SERVER_ADDR' => string '175.16.0.2' (length=10)
'SERVER_PORT' => string '80' (length=2)
'REMOTE_ADDR' => string '175.14.0.1' (length=10)
'DOCUMENT_ROOT' => string '/var/www/html' (length=13)
'REQUEST_SCHEME' => string 'http' (length=4)
'CONTEXT_PREFIX' => string '' (length=0)
'CONTEXT_DOCUMENT_ROOT' => string '/var/www/html' (length=13)
'SERVER_ADMIN' => string 'webmaster@localhost' (length=19)
'SCRIPT_FILENAME' => string '/var/www/html/75...' (length=43)
'REMOTE_PORT' => string '62844' (length=5)
'GATEWAY_INTERFACE' => string 'CGI/1.1' (length=7)
'SERVER_PROTOCOL' => string 'HTTP/1.1' (length=8)
'REQUEST_METHOD' => string 'GET' (length=3)
'QUERY_STRING' => string '' (length=0)
'REQUEST_URI' => string '/75%20Global%20Variables/' (length=25)
'SCRIPT_NAME' => string '/75 Global Variables/index..' (length=30)
'PHP_SELF' => string '/75 Global Variables/index.php' (length=30)
'REQUEST_TIME_FLOAT' => float 1666177056.2562
'REQUEST_TIME' => int 1666177056
$_GET
Grabs the data from the URL parameters that are passed to the script. For example, dinocajic.com/?page=contact. In this instance, page
would be added as the key and contact
would be the value that’s displayed when you call $_GET['page']
.
$_POST
Similar to the $_GET
associative array, the $_POST
associative array contains key/value pairs from form data that’s submitted. For example, when a user enters data in the input field <input type="text" name="first_name">
, the data is stored in the $_POST
array. Calling $_POST[‘first_name’]
yield whatever you entered, i.e. Dino
.
$_FILES
We haven’t looked at file submission yet, but when files are submitted via a form, you can access file data via the $_FILES
associative array. We’re going to look at uploading files in the next article.
$_REQUEST
Contains the content of $_GET
, $_POST
, and $_COOKIE
associative arrays. That means that you can use both $_REQUEST
and $_POST
to access the same names, for example.
$_SESSION
Stores session data. We’re going to look at storing data on the server that’s accessible across different scripts for a specific user session soon.
$_COOKIE
Sessions store data on the server side and cookie variable allows for data to be retrieved from the client side. We’re going to look at the differences between session and cookie data at a later article.
$_ENV
Stores environmental variables. When we look at Laravel, environmental variables are used to setup configuration of the app and are quite useful.
Summary
These are some of the most common PHP predefined variables. For a full list, read the PHP docs. I wanted to list these out here as an introduction for future articles and because we covered form submission in the previous article.
GET FETCHES DATA FROM A SERVER, WHILE POST SENDS DATA TO IT
PHP – P75: GET AND POST REQUEST METHODS
GET and POST requests are nit unique to PHP, but it’s still something that you need to understand. HTTP requests allow for communication between the client and the server.
Access server insights with PHP’s built-in predefined variables
PHP – P76: predefined variables
PHP automatically has access to form submission variables since they’re added to the $_POST variable. Let’s dive into the most common variables like $_SERVER, $_GET, and $_POST.
Access server insights with PHP’s built-in predefined variables
PHP – P77: basics of file uploading
One of the most misunderstood topics in PHP for beginners. How does someone upload a file? What happens when you click upload? Where does it go? Let’s demystify this process and tackle files in PHP. It’s simpler than you think.