PHP Code:
setcookie ('country','USA')
Possible arguments
expiration time
PHP Code:
setcookie ('country','USA',1071448497)
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)
PHP Code:
setcookie ('country','USA','','/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 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)
Deleting a cookie
Just call setcookie with no value and a expiration time in the past
PHP Code:
setcookie ('country','',time()-86400)
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']";
}
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


Search
Categories


Print Article
Send to a friend
Save as PDF