PHP cookies

PHP cookies

A cookie is used for identification purposes. It is more commonly used to identify a user in a session. It is a small file the application inserts on the users computer. With PHP one can create and retrieve the cookie.

Setting cookie in php:

Cookies in PHP can be set using the setcookie() function. This must appear before the HTML tag.

Syntax:
Setcookie(name, value, expire, path, domain);

Example:
here, the cookie name sample is assigned a value jim. The cookie expires after an hour.
Setcookie(“sample”, “jim”, time()+3600);

Retrieving cookie value:

The cookie that is set can be retrieved as shown below:
Echo $_cookie[“user”];

Isset() function can be used to find if the cookie is set.

What is a Persistent Cookie?

Cookies are used to remember the users. Content of a Persistent cookie remains unchanged even when the browser is closed. ‘Remember me’ generally used for login is the best example for Persistent Cookie.

How to set cookies? How to reset/destroy a cookie?

Setting cookie in php:

Cookies in PHP can be set using the setcookie() function. This must appear before the HTML tag,

Syntax:
Setcookie(name, value, expire, path, domain);

Example:
Here, the cookie name sample is assigned a value jim. The cookie expires after an hour.
Setcookie(“sample”, “jim”, time()+3600);

Reset / destroy cookie:

Cookies can be deleted either by the client or by the server. Clients can easily delete the cookies by locating the Cookies folder on their system and deleting them. The Server can delete the cookies in two ways:

- Reset a cookie by specifying expiry time
- Reset a cookie by specifying its name only

How to set cookies in PHP?

The function setcookie() is used to define a cookie that is to be sent along with HTTP headers. The cookie must be sent prior to any output from the script as is the protocol restriction. After setting the cookies, they can be used when the next page is loaded by using $_COOKIE or $HTTP_COOKIE_VARS arrays.
PHP sessions
PHP sessions introduction - When a user logs in an application, his details are usually stored in a session variable.....
PHP security
PHP security tips - Avoid the use of global variables. Hence it must be ensured that register_globals option is not enabled.....
PHP handling file uploads
PHP handles file uploads through different method. POST method uploads: This allows user to upload both text and binary files..
Post your comment
Discussion Board
Difference between Session and Cookies?
Hi, I preffered Seesion because it is safer than cookies.
Session:-
1) Session is created on Server-side.
2)This method is safe, because your data is stored on server side and hacker can’t hack your data.
3)Session is destoryed when you close your browser.
4)You can’t set the closing time for Session.
5) You hust create the Session_variable for your data to store in the session.

Cookies:-
1)Cookies is created on Client-side.
2)This method is not safe, because your data is stored on your own machine and hacker can hack your data.
3)Cookies is not destroyed even if you close the brwoser.You can open it within2-days.
4)You can set closing time for Cookies.
5) Cookies(name,value,time,path,domain)
Cookies(Gopal,123,time()+3600)
Here we create a cookie name=Gopal, Vlaue=123 and Time= 1 hour.
Gopal Sharma 07-3-2015
ans:
sir,i think it is possible to use persistent property....by using this property we can set the time what we want
kiran 12-4-2012
persistent cookie
hi , i am building a web site. here when a user login and if he doest log out and closes the browser and if he visit after 2 or three days after , he should be logged in without entering username anuser namerd.
how cookie can be programmed to perform in this way and is this kind of thing can be seen in any websites.? if so, please provide me reference.
manoj 11-22-2011