PHP Cookies

 Cookie is a small piece of information stored as a file in the user's browser by the web server. Once created, cookie is sent to the web server as header information with every HTTP request.

You can use cookie to save any data but it should not exceed 1K(1024 bytes) in size.

The web works based on the HTTP protocol. The HTTP protocol is stateless.

When the web browser requests a page from a web server, the web server responds with the page content. Later, the same web browser requests the same page again, the web server has no information that the request is from the same web browser.

Cookies solve this stateless challenge.

A cookie is a piece of data that a web server sends to the web browser. The web browser may store it and send it back in the subsequent requests to the same web server. By using the same cookie, the web server knows that two requests come from the same web browser.

Cookies are also known as web cookies, HTTP cookies, or browser cookies. We’ll use the cookies to make it short.

How Cookie works

First, the web browser sends a request to the web server. The web server doesn’t have any information about the web browser. The web server creates a cookie with a name return and a value 1 and attaches the cookie to the HTTP response header. To create a cookie, you’ll use the setcookie() function.

Second, the web browser stores the cookie.

Third, the web browser sends the second request with the stored cookie in the header of the HTTP request to the web server. On the web server, PHP can access the cookie via the $_COOKIE superglobal variable and do something accordingly.

Finally, the web server responds with the content of the request. Typically, it responds to the web browser with the content based on the value of the cookie.

A web browser can store a cookie with a maximum size of 4KB. However, it’s different between web browsers.

A cookie has an expiration date. Typically, web browsers store cookies for a specific duration. And the web server can specify the expired time for a cookie.

A cookie also stores the web address (URL) that indicates where it comes from. And the web browser only sends back the cookie that was originally set by the same web address. In other words, a website won’t be able to read a cookie set by other websites.

Most modern web browsers allow users to choose to accept cookies. Therefore, you should not completely rely on cookies for storing critical data.

Types of Cookies

There are two types of cookies, they are:

Session Cookie: This type of cookies are temporary and are expire as soon as the session ends or the browser is closed.

Persistent Cookie: To make a cookie persistent we must provide it with an expiration time. Then the cookie will only expire after the given expiration time, until then it will be a valid cookie.

Creating a Cookie in PHP

In PHP we can create/set a cookie using the setcookie() function.

Below we have the syntax for the function,

setcookie(name, value, expire, path, domain, secure)

The first argument which defines the name of the cookie is mandatory, rest all are optional arguments. Let's understand what are the available arguments that we can supply to the setcookie() function to set a cookie.

name - Used to specify the name of the cookie. It is a mandatory argument. Name of the cookie must be a string.

value - Used to store any value in the cookie. It is generally saved as a pair with name. For example, name is userid and value is 7007, the userid for any user.

expire - Used to set the expiration time for a cookie. if you do not provide any value, the cookie will be treated as a session cookie and will expire when the browser is closed.

path - Used to set a web URL in the cookie. If set, the cookie will be accessible only from that URL. To make a cookie accessible through a domain, set '/' as cookie path.

domain - The domain of your web application. It can be used to limit access of cookie for sub-domains. For example, if you set the domain value as wwww.blogger.com, then the cookie will be inaccessible from wartalab.blogspot.com.

secure - If you set this to 1, then the cookie will be available and sent only over HTTPS connection.

So if we want to create a cookie to store the name of the user who visited your website, and set an expiration time of a week, then we can do it like this,

<?php

setcookie("username", "xyz", time()+60*60*24*7);

?>

To access a stored cookie we use the $_COOKIE global variable, and can use the isset() methos to check whether the cookie is set or not.

Let's have a complete example where we will set a cookie and then retrieve it to show its value in the HTML page.

<?php

// set the cookie

setcookie("username", "iamabhishek", time()+60*60*24*7);

?>

<html>

    <body>

    <?php

    // check if the cookie exists

    if(isset($_COOKIE["username"])) {

        echo "Cookie set with value: ".$_COOKIE["username"];

    } else {

        echo "cookie not set!";

    }

    ?>

    </body>

</html>

So by providing the name of the cookie inside the square brakets with the global variable $_COOKIE[] we can access the cookie.

NOTE: setcookie() function should be placed before the starting HTML tag(<html>).

No comments:

Write a program in PHP to reverse a number

A number can be written in reverse order. For example 12345 = 54321 <?php   $ num = 23456;   $ revnum = 0;   while ($ num > 1)   {   $...