School of Computing  |  Napier  |  Cisco  |  SFC  |  NOS  |  Code |  Old page

PHP - Cookies

Personalization can be achieved in number of ways, from simple cookies, which are stored on the users computer to logging into a server (in the way which MSN Messenger does). Cookies are simple text files (typically stored as TXT files in the WINDOWS\COOKIES folder on Microsoft Windows), and will contain relevant details on the user, and the any of their preferences. As these are text files, they cannot do any damage to the local computer. Some-times users delete these cookies, and all the previous information is lost, and must thus re-register for the system to be able to store their details. PHP transparently supports HTTP cookies.

Cookies are defined in the HTTP header, and must thus to specified before the HTML content. The format of the cookie statement in the header is of the form:

"Set-Cookie: billCookie=Test expires =Tuesday 10-Apr-2005 00:00:00 GMT;"

PHP allows either the header statement to be used, such as:

header("Set-Cookie: billCookie=Test expires =Tuesday 10-Apr-2005 00:00:00 GMT;")

or with the setcookie function:

setcookie("billCookie", "Test", time()+3600)

which specifies that the cookie expires in 3600 seconds (which is 1 hour from the current time). The format of the setcookie() function is:

int setcookie ( string name [, string value [, int expire [, string path
[, string domain [, int secure]]]]])

which must be placed before any HTML tags. The path defines the location of the cookie, and the domain defines the domain which can read from the cookie. The domain disallows other WWW servers from reading from the cookie which was created by another domain. The following code can be added to the top of an HTML file, and will create a cookie:

Character conversions

<?php
setcookie("billscookie","Test",time()+3600);
/* Expire in 1 hour */
?>

Executed code

Note this is set in the Header. So it won't be shown here! Have a look in the \WINDOWS\ COOKIES directory, and see if it is there.

Determining if there is a cookie present:

Character conversions

<?php
if (isset($billscookie))
{
print "<P>A cookie exists on your computer";
}
?>
The cookie should contain something like:
billscookie
Test
localhost/
1024
142822656
29437464
2805528320
29437455
*


Executed code

Reading a cookie:

Reading a cookie

<?php
if (isset($billscookie))
{
if ($billscookie == "Test")
{
print "<P>Welcome back";
}
else
{
print "<P>Nice to see you!";
}
}
?>


Executed code

A cookie can be deleted by creating a new cookie with the same name as the one which is to be deleted, but setting the expiry time as some time in the past. This will automatically create, and then delete the cookie, such as:

Deleting a cookie

<?php
setcookie("billCookie", "Test", time()-60) // delete cookie
?>


Executed code

 

Note that this statement must be placed at the top of the HTML/PHP file, before any HTML statements.

 

School of Computing  |  Napier  |  Cisco  |  SFC  |  NOS  |  Code |  Old page

Page maintained by bill