PHP Tutorial: Requests

Alright, so for this PHP tutorial, we’ll be covering requests.

Basics

The fundamental of the web and how it works is that you are able to send GET and POST requests to a web page. For the front-end, method of fetching and transferring data are usually set in HTML forms. Here’s an example usage of setting GET and POST methods in forms.

<form method="POST" action="login.php">
	<input type="text" name="username" placeholder="Username">
	<input type="password" name="password" placeholder="Password">
</form>

What about normal visiting of websites? Well, whenever you visit a web page through your web browser, your browser will send a GET request to that website to fetch its headers and content.

php-requests-example

2015-09-26_18-39-19An example of a HTTP GET request to Lowendtalk.com

I visited the website “http://www.lowendtalk.com”, so my browser made a HTTP GET request to that website. Okay, so let’s breakdown the first example shown above.

php-requests-example-2

The method is GET, which means yeah, it’s a GET request. As for URI, it’s the “Uniform Resource Identifier”, which means the name of the resource being requested. In this example, the resource being requested from the site is “/”. It’s a bit confusing so I’ll show another example later. The version refers to the version of the request being made. HTTP/1.1 is a newer version of the HTTP protocol and right now, the latest HTTP protocol version being implemented is HTTP/2.0 which most websites do not support, yet. The host refers to the hostname of the website and in this case, it’s “www.lowendtalk.com”.

php-requests-example-3

So here’s the 2nd example. By now, you should be clear about what part does the URI refers to. Now, we wanna try extracting some data in PHP.

You can observe that the URI has some funny looking characters like “?” and “=”. The question mark means it is the start of a query string. “Wait wait wait, what’s a query string?” Well, query string is the content that is included for data processing. It is used typically in HTML forms to submit data to the server.

Extracting Data

Okay okay, now let’s seriously move on to the PHP coding part. As you can see in the query string, “q=ayylmao”. This means the key is “q” while the value for the key “q” is “ayylmao”. So how do we extract the value “ayylmao”? It’s pretty simple actually.

<?php
echo $_GET["q"]; //this will output "ayylmao"
?>

So we’ve covered GET requests, what about POST requests? According to RFC 2616 [1], a GET request should be used for retrieving data from the server whilst POST request should be used for sending data to the server. But both of them are quite similar to each other in most ways. The main difference is that for GET requests, the data is shown in the browser URL but for POST requests, it isn’t. On top of that, GET requests are usually cached by your browser while POST requests are never cached.

Since GET requests would show your entire query string in the browser URL, you would usually want to use POST requests for data sensitive data submissions such as a login or registration form [2]. This is because your friend sitting beside you would know your password if the login form’s method is a GET. However, for a search form, a GET request will do just fine.

So let’s say the request made was a POST request, the query string data can be extracted like this.

<?php
echo $_POST["q"]; //this will output "ayylmao"
?>

So to just get you a visual representation of what I’ve just said above, here’s an image.

difference-between-get-and-post

I hope you’ve understood what requests are and the differences between a GET and POST request. If you have any other questions, feel free to leave a comment below and I’ll get back to you as soon as possible. Have fun with PHP! :)

Next Tutorial: PHP Sessions

Tutorial Overview: Fast Track Learning

Sources Cited:

[1] – http://tools.ietf.org/html/rfc2616#page-53
[2] – http://tools.ietf.org/html/rfc2616#section-15.1.3

Author: Woo Huiren

Howdy, I'm a IT student who is currently serving my full time national service. I do lots of things related to IT but I have an exceptionally great love for the web and opensource, so expect me to blog a lot about it! :)

Leave a Reply