Cookies in PHP

Cookies in PHP

Cookies in PHP
—————-

The main use for cookies is to solve the problem of lack of status when browsing web pages.

With cookies, small portions of information are embedded in the browser, allowing the identification of cookies in several pages from the same site or

even during visits between several days.

Actually, cookies are nothing but text strings that are sent from the client server (web browser) and embedded in the server, and then the browser sends

these cookies to the server allowing the identification of the client in the server.

How to use Cookies
———————-

In PHP, Cookie is set by using the setcookie Statement,

Syntax :
——–

int setcookie (string Name [, string Value [, int Expire [, string Path [, string Domain [, int Secure]]]]])

Setcookie() defines a cookie that is sent along with the rest of the information from the header. Cookies shall be sent before any html tag;

therefore, we shall call one of these statements before any tag or . This is a restriction of cookies, not of PHP.
All messages, except name, are optional.

* Name. Name of the cookie. If we create a cookie only with its name, the cookie existing in the client under said name will be deleted. We

can also replace any argument with an empty string(”").

* Value. Value to be stored by the cookie in the client.

* Expire. The argument expire is an integer argument that indicates the time a cookie will be deleted in the time format returned by the UNIX statements time() and mktime(). Time() + N seconds of duration is generally used to specify the duration of the cookie.

* Path. Subdirectory where the cookie has a value.

* Domain. Domain where the cookie has a value. If we establish www.domain.com as domain, the cookie is not set for domain.com.

Meanwhile, if we establish domain.com as domain, the cookie is set as for domain.com as for www.domain.com.

* Secure. The message secure indicates that the cookie will only be set by a secure HTTPS connection.

Example:
———

setcookie(”username”,”abc”);

here, username is a name, the value is abc. Now, cookie is set.

It is retreiving by using $_COOKIE.

echo $_COOKIE[’username’];
It displays abc.

setcookie(”username”,”abc” time()+3600);

In this example, we set a user name cookie that has the value abc, But the value is only availabe for hour. that means the cookies is expired after 1 hour.

Leave a Reply


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.