SCSI Hard Drives
Search
Advanced Search

Categories


Recently Viewed
Pages



My Links
Web Directory Index
A human edited, comprehensive web directory list.
Link Exchange
DesignFirms Link Exchange


Creating and Managing cookies in PHP

By : sterex
Rating : Not Rated


Setting a cookie

PHP Code:
setcookie ('country','USA'
Setcookie() must be called before any output is made, since cookies are sent with the HTTP headers.

Possible arguments

expiration time

PHP Code:
setcookie ('country','USA',1071448497
The third argument is the expiration time, this is expressed as an epoch time stamp ie 7 am, 15/12/2045)
If this argumetn is empty (or missing) the cookie will expire when the browser is closed.
To set a expiration time for example after one hour you would use:

PHP Code:
setcookie ('country','USA',time()+3600)

time() reads the current time
+3600 adds 3600 seconds (aka 1 hour
Path

PHP Code:
setcookie ('country','USA','','/locations/'
The fourth argument indicates that the cookie is only sent back to the server when pages are requested whose path begins with the argumented string. (in this case /locations/)

Note that the cookie doesn't have to be set from this path.

Domain

PHP Code:
setcookie ('country','USA','','','.buildtolearn.com')
setcookie ('country','USA','','','yankees260an.buildtolearn.com')
setcookie ('country','USA','','','buildtolearn.com'
The fifth argument is sent back to the server only when pages whose hostname ends with the argumented domain.
The first example would sent the cookie to all sites ending with buildtolearn.com (eg: nkzd.buildtolearn.com as well as pkzone.buildtolearn.com)
The second example only sent it to yankees260an.buildtolearn.com
The third one would only sent it to Buildtolearn.com (and not to www.buildtolearn.com or netfo.buildtolearn.com)

SSL connection flag

PHP Code:
setcookie ('country','USA','','','',1
The sixth and last argument is a flag (0 or 1). If set 1 it will only send a cookie over an SSL (secure) connection. This is handy when the cookie contains sensitive information.

Deleting a cookie

Just call setcookie with no value and a expiration time in the past

PHP Code:
setcookie ('country','',time()-86400
Use an expiration time with a couple of hours or an entire day (86400 seconds). This is in case the server and the user's computer have unmatching clocks (server in America has different time with user's computer in Germany for example)
The setcookie() for deleting has to have the same arguments that the setcookie() used for setting the cookie (except for value and time).

Reading values of cookies

PHP Code:
if (isset($_COOKIE['country'])) {
print 
"I live in $_COOKIE['country']";

Cookie values are stored in the superglobal array $_COOKIE.
The $_COOKIE only stores the value of a cookie since the browser only sends this back to the server, not the expiration, path, domain and the secure status. These can't be read.

note: the set_cookie() function doesn't change the value of $_COOKIE. So during the request in which the cookie is set, the value of the cookie can't be found in $_COOKIE







Comments / Feedback

RSS 2.0: Syndicate this article

Add Comment
* Name


* Email Address


Site



*Image Validation (?)


*Comments / Feedback





Print Article Print Article Send to a friend Send to a friend Save as PDF Save as PDF Social Bookmarking
Add to: Mr. Wong Add to: Webnews Add to: Icio Add to: Oneview Add to: Folkd Add to: Yigg Add to: Linkarena Add to: Digg Add to: Del.icio.us Add to: Reddit Add to: Simpy Add to: StumbleUpon Add to: Slashdot Add to: Netscape Add to: Furl Add to: Yahoo Add to: Spurl Add to: Google Add to: Blinklist Add to: Blogmarks Add to: Diigo Add to: Technorati Add to: Newsvine Add to: Blinkbits Add to: Ma.Gnolia Add to: Smarking Add to: Netvouz Information
Rate this Article :

1

2

3

4

5

6

7

8

9

10
Poor Excellent