PHP

COOKIES IN PHP

Cookies: Simply it is the information stored in the user’s computer by the Web Server.
Information stored:

  • String of text that contains information about the browser
  • Some cookies store personal information about the user

Need:

  1. To identify the user and possibly prepare the customized website or to save site login information of the user.
  2. The website can remember the user and load his preferences whenever that particular user access that website again.

Cookies Capabilities
Instead of seeing just a generic welcome page the user might see a welcome page with his/her name on it.

Types of Cookies

Session cookie
Also called a transient cookie, a cookie that is erased when the user closes the Web browser. The session cookie is stored in temporary memory and is not retained after the browser is closed. Session cookies enable the website that the users are visiting to keep track of their movements from page to page so that they don’t get asked for the same information which they have already given to the site. The most common example of this functionality is the shopping cart feature of any ecommerce site.
Persistent cookie
Also called a permanent cookie, or a stored cookie, a cookie that is stored on the user’s hard drive until it expires (persistent cookies are set with expiration dates) or until one deletes the cookie. Persistent cookies help websites remember user’s information and settings when any user visits them in the future. This results in faster and more convenient access since, for example, the user don’t have to login again.
Features made possible by persistent cookies include: language selection, theme selection, menu preferences, internal site bookmarks or favourites, among many others.
Cookies In PHP
A cookie is created with the setcookie() function.
Syntax

setcookie(name,value,expiry,path,domain,secure,httponly)

Only the name parameter is required. All other parameters are optional.
set.php

<?php
setCookie(“username”,”Anuj”,);
?>

view.php

<?php
echo “The cookie ”.$_COOKIE[username]”.is set”;
?>

Output of set.php after set.php is opened
The cookie mudit is set
Note!!:
If the html coding lies within the same php page, then setcookie() should appear before the tag.
Modifying a cookie- very simple just set the cookie again using the setcookie() function.
Deleting a cookie: Give the expiry time parameter while setting the cookie.

<?php
// set the expiration date to one hour ago
setcookie("user", "", time() - 3600);//time()-get the timespan
?>
<html>
<body>
<?php
echo "Cookie 'user' is deleted.";
?>
</body>
</html>

Drawbacks of cookie-

  1.  Cookies can be manipulated by the client. This causes security issues.
  2. The server can be sent wrong values by the user.
  3.  As they are being stored in the client machine, they can be deleted intentionally as well as mistakenly by the user
  4. Persistent cookies will require memory space of the client machine to be stored

Recommended Tutorials for you


Related posts

Array in PHP

Anuj Kumar

Store Procedure In PHP

Anuj Kumar

PHP – PECL

Anuj Kumar

How to insert data into MySql using PHP

Anuj Kumar

PHP Prepared Statements

Anuj Kumar

Conditional Statement

Anuj Kumar

How to append a string in PHP

Anuj Kumar

Sorting Arrays in PHP

Anuj Kumar

How to get yesterday and tomorrow date in php

Anuj Kumar

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.