Tuesday, April 12, 2011

PHP Cookies by Example

PHP Cookies by Example
Cookie is a Dutch word koekje or (informal) koekie which means little cake, and arrived in the English language through the Dutch in North America.

In the United States and Canada, a cookie is a small, flat-baked treat, usually containing fat, flour, eggs and sugar. In most English-speaking countries outside North America, the most common word for this is biscuit; in many regions both terms are used, while in others the two words have different meanings. A cookie is a plain bun in Scotland,[1] while in the United States a biscuit is a kind of quick bread similar to a scone. In the United Kingdom, a cookie is referred to as a baked biscuit most commonly containing chocolate chips.

you can see my love for cookies, let move to the point :)

HTTP Cookies

The term "cookie" (in IT) was derived from "magic cookie", which is a packet of data a program receives and sends again unchanged.

A cookie, also known as a web cookie, browser cookie, and HTTP cookie, is a piece of text stored on a user's computer by their web browser. A cookie can be used for authentication, storing site preferences, shopping cart contents, the identifier for a server-based session, or anything else that can be accomplished through storing text data.

almost all web programming languages provide support for Cookies, PHP is no exception.

Cookies in PHP

To create and modify a cookie, use the PHP function setcookie(). setcookie() takes up to seven arguments, depending upon how much control you want over the cookie and who can read its value. To read value from Cookies you set you use $_COOKIE global array provided by PHP

Method Signature of setcookie();
bool setcookie ( string $name [, string $value [, int $expire = 0 [, string $path [, string $domain [, bool $secure = false [, bool $httponly = false ]]]]]] )
name:The name of the cookie.

value:The value of the cookie. This value is stored on the clients computer; do not store sensitive information.

expire:The time the cookie expires. This is a Unix timestamp so is in number of seconds since the epoch. In other words, you'll most likely set this with the time() function plus the number of seconds before you want it to expire. Or you might use mktime(). time()+60*60*24*30 will set the cookie to expire in 30 days. If set to 0, or omitted, the cookie will expire at the end of the session (when the browser closes).
path:The path on the server in which the cookie will be available on. If set to '/', the cookie will be available within the entire domain. If set to '/foo/', the cookie will only be available within the /foo/ directory and all sub-directories such as /foo/bar/ of domain. The default value is the current directory that the cookie is being set in.

domain:The domain that the cookie is available to. To make the cookie available on all subdomains of example.com (including example.com itself) then you'd set it to '.example.com'. Although some browsers will accept cookies without the initial ., » RFC 2109 requires it to be included. Setting the domain to 'www.example.com' or '.www.example.com' will make the cookie only available in the www subdomain.

secure:Indicates that the cookie should only be transmitted over a secure HTTPS connection from the client. When set to TRUE, the cookie will only be set if a secure connection exists. On the server-side, it's on the programmer to send this kind of cookie only on secure connection (e.g. with respect to $_SERVER["HTTPS"]).

httponly:When TRUE the cookie will be made accessible only through the HTTP protocol. This means that the cookie won't be accessible by scripting languages, such as JavaScript. This setting can effectively help to reduce identity theft through XSS attacks (although it is not supported by all browsers). Added in PHP 5.2.0. TRUE or FALSE

Now Let the code Talk

Simplest way to make a Cookie.
setcookie('name', 'value');
To let cookie expire after a certain time we use mktime() to make time, it gives us time in seconds.
$expireTime=mktime(0,0,0,4,13,2011);
setcookie('name', 'value',$expireTime);
The next two parameters for setcookie() let you control the path and the domain of who can read your cookie. By default, only pages equal to or lower down in the hierarchy on the same server that sends the cookie can read its value. That's for security's sake. However, if you had an account that's sometimes "www.domain.com" but also "other.domain.com," and your account lets you serve pages from ~/myhome, you should modify setcookie() as such.
$expireTime=mktime(0,0,0,4,13,2011);
setcookie('name', 'value',$expireTime,'~/PATH', '.tutorialjinni.com');
to read a Cookie
echo $_COOKIE['NAME_OF_COOKIE'];
// print value of specified Cookie
to Delete a cookie just pass the name of the cookie you want to delete
setcookie("NAME_OF_THE_COOKIE_WHOM_TO_DELETE");
//deletes the specified cookie
Enjoy Cookies :)

1 comments:

Best PHP Training on April 27, 2011 at 11:11 PM said...

Author

Tutorial jinni modify the real meaning of cookies like Cookie is a Dutch word koekje or (informal) koekie which means little cake ,A cookie is a plain bun in Scotland after that easily let move to the point :) PHP cookie so by all means they want to explain the PHP cookie . very nice tutorial i like it very much.Thanks for it

Post a Comment

 

Blog Info

A Pakistani Website by Originative Systems

Total Pageviews

Tutorial Jinni Copyright © 2015 WoodMag is Modified by Originative Systems