Home > PHP > Have some Cookies!!

Have some Cookies!!

Q: What are cookies?

Ans: A Cookie is a small amount of data stored on the client’s browser in compliance with a request from a server or script. The information is constantly passed in HTTP headers between the browser and web server; the browser sends the current cookie as part of its request to the server and the server sends updates to the data back to the user as part of its response. Once a cookie is created, it will expire after a specified time period.

Q: What is the need of cookies?

Ans: HTTP is a stateless protocol. Therefore, every-page a user downloads from your server represents a separate connection.

1-Cookies allow you to do neat things, like automatically log a user into your site when he loads a page in his browser, or store temporary information about your user, such as what items he has in a shopping cart.

2- Allows a user to remain ‘logged on’ until he explicitly logs out or the browser window is closed.

3- After a cookie is set, only the originating host can read the data, ensuring that the user’s privacy is respected.

Q: How we can set a cookie?

Anw: You can set a cookie in a PHP script in two ways:

Using header() function:

header(“SET-COOKIE: newcookie=mycookie; expires=Sat, 26-Aug-06 14:40:28 GMT; path=/; domain=corrosive.co.uk”);

Using setcookie() function:

setcookie(name, value, expiration, path, domain, secure, httponly);

Example:

<?php

setcookie(“newcookie”, “mycookie”, time()+3600, “/”,”corrosive.co.uk”, 0);

?>

<html>

<head><title>Setting a new cookie</title></head>

<body>

<?php if(isset($_COOKIE[‘newcookie’])) {

echo “<p> Hello again, your chosen cookie is “;

echo “{$_COOKIE[‘newcookie’]} </p>”;

} else {

echo “<p> Hello you. This may be your first visit</p>”;

} ?>

</body>

</html>

Q: How to access the already set cookie?

Ans: With the help of the superglobal variable $_COOKIE[‘cookie_name’]

Q: How to determine the expiration of the cookie?

Ans: The “expiration” variable in the setcookie() function decides the expiration of the cookie. Here are some amount of time which signify the determination of the expiration time/date of a cookie

One Minute: 60
Ten Minutes: 600
Half-an-Hour: 1800
One Hour: 3600
One Day: 86400
One Week: 604800
Two Weeks: 1209600
One Month (30 days): 2592000
One Year (365 days): 31536000

Q: How to delete a cookie?

Ans: There are two ways to reset/destroy a cookie:

Reset a cookie by specifying already expired time:-

setcookie(“newcookie”, “”, time()-60, “/”, “corrosive.co.uk”, 0);
Reset a cookie by specifying its name only:-

setcookie(“newcookie”);

Q: How we can create the session cookie?

Ans: To create a cookie that lasts only as long as the user is running the browser, pass setcookie() an expiry argument of 0.

setcookie(“session_id”, “8832”,0);

The browser does not remember this cookie after it has been quit or restarted.

Advertisements
Categories: PHP
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: