| 1. |
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. |
|
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‟s 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‟s 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 ?> <html> <body> <?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]; } ?> </body> </html> |
|