PHP, HTTP, and the web

(Last Updated On: December 30, 2018)

1. Introduction

The majority of our web works on a client-server model which is governed by the HTTP protocol. In this article, we are having an introductory look on HTTP protocol and different capabilities of PHP related to HTTP. Also, we are exploring some of the most useful and essential functions in PHP to create a web application which runs over the web using the HTTP protocol. So, let us start.

2. HTTP Protocol

HTTP stands for HyperText Transfer Protocol. HTTP is the protocol used by the internet or WWW. HTTP defines how messages are formatted and transmitted between two different systems connected on the internet and what actions servers and client software like browsers should take in response to various commands. The client (most of the times a browser) sends a request to server and server reads the request and sends back the expected response to the client.

3. $_REQUEST, $_GET and $_POST

We have learned about the superglobals in earlier articles. $_REQUEST contains all the information about the request that a client sends to the server. $_GET contains only the information about the GET request and similarly $_POST contains information only about POST request. So, we can say that all the information that is present in $_GET and $_POST is also available in $_REQUEST. To understand these variables deeply, let’s assume that there is an HTML form:
<form method="POST" action="handle.php">
    <input name="name" />
    <button type="submit">Submit</button>
</form>

When a user submits it, it sends a POST request to the script handle.php. Now, assume that the code of handle.php is:

<?php
    print_r($_POST);
?>

the handle.php produces the result as below on the output:

array(
    name => bootsity
)

4. Cookies

The HTTP protocol is a stateless protocol. To identify any subsequent requests from the same client, we share cookies with a client and client attaches those cookies in subsequent requests. Cookies are tiny files that are generated by the server and send back in response, and the client sends those cookies in all subsequent requests. In PHP, we can use setcookie function to create a cookie:
setcookie(name, value, expire, path, domain, secure, httponly);

5. Session

When we work with an application, we open it, do some changes, and then we close it. This is much like a Session. The system knows who we are. It also knows when we start the application and when you end. However, on the internet, as HTTP is a stateless protocol, there is a problem: the web server does not know who we are or what we do, because the HTTP address doesn’t maintain state. To solve this difficulty, we make use of sessions. The session is a storage area on the server shared between the different request from a particular client. In PHP, we can start using the session using a function:
session_start();

6. Conclusion

In this article, we learned about how HTTP works in client-server model followed by www. We also learned about the different capabilities of PHP related to the HTTP protocol and how these functionalities can be used in creating a web application in PHP. In the next article in this series, we will learn about databases and how we can use them as data storage systems and access them using PHP programming language.

LEAVE A REPLY

Please enter your comment!
Please enter your name here