+3 votes
in Computer by kratos

What is a cookie? Write the PHP code to set a cookie named “car” and value “BMW”. The cookie should expire after two days. Also Display the value stored in $_COOKIE.

1 Answer

+5 votes
by kratos
 
Best answer

A cookie is a small piece of data that the web server sends to a web browser so that the web server can keep track of user‟* activity on a particular website.

When a user uses a computer to visit a website, the website stores some basic information about the visit on the hard disk of the computer. It records the user‟* preferences while using the site. This stored information is called a "cookie".

<?php

$cookie_name = "car";

$cookie_value = "BMW";

setcookie($cookie_name, $cookie_value, time() + (86400 * 2), "/"); // 86400 = 1 day ?>

<?php if(!isset($_COOKIE[$cookie_name])) { echo "Cookie named '" . $cookie_name . "' is not set!"; } else { echo "Cookie '" . $cookie_name . "' is set!<br>"; echo "Value is: " . $_COOKIE[$cookie_name]; } ?>
...