Docsity

PHP Cookies and Sessions: A Comprehensive Guide, Cheat Sheet of Web Programming and Technologies

Partial preview of the text

Download PHP Cookies and Sessions: A Comprehensive Guide and more Cheat Sheet Web Programming and Technologies in PDF only on Docsity!

The need for persistence

Consider these examples

 Counting the number of “ hits ” on a website

 i.e. how many times does a client load your web page

source

 The questionnaire on computing experience

Somehow your .php needs to remember previous

instances of it being requested by a client

Persistence and HTTP

Recall http is a stateless protocol. It remembers nothing about

previous transfers

Two ways to achieve persistence:

 PHP cookies

 PHP sessions

HTTP

server Clien t Cookie Session

HTTP Cookies

In internet programming, a cookie is a packet of information sent from the

server to client, and then sent back to the server each time it is accessed by

the client.

Introduces state into HTTP (remember: HTTP is stateless )

Cookies are transferred between server and client according to http.

PHP supports http cookies

Cookies can also be thought of as tickets used to identify clients and their

orders

setcookie (name,value,expire,path,domain,secure) Parameter Description name (Required). Specifies the name of the cookie value (Required). Specifies the value of the cookie expire (Optional). Specifies when the cookie expires. e.g. time()+36002430** will set the cookie to expire in 30 days. If this parameter is not set, the cookie will expire at the end of the session (when the browser closes). path (Optional). Specifies the server path of the cookie. If set to " / ", the cookie will be available within the entire domain. If set to " /phptest/ ", the cookie will only be available within the test directory and all sub-directories of phptest. The default value is the current directory that the cookie is being set in. domain (Optional). Specifies the domain name of the cookie. To make the cookie available on all subdomains of example.com then you'd set it to ".example.com". Setting it to www.example.com will make the cookie only available in the www subdomain secure (Optional). Specifies whether or not the cookie should only be transmitted over a secure HTTPS connection. TRUE indicates that the cookie will only be set if a secure connection exists. Default is FALSE.

Cookies from HTTPCookies from HTTP

GET /.html HTTP/1. Host: it026954.domain GET /.html HTTP/1. Host: it026945.domain Cookie: name=value Accept: /

HTTP /1.1 200 OK

Content-type: text/html Set-Cookie: name=value (content of page) Client (e.g. Firefox) it

Creating cookies with setcookie()

Use the PHP setcookie() function:

Setcookie (name,value,expire, path, Setcookie (name,value,expire, path, domain,domain, secure)secure) e.g.

php

setcookie("MyCookie", $value, time()+3600*24);

setcookie("AnotherCookie", $value, time()+3600);

Name: name of the file

Value: data stored in the file

Expire: data string defining the life time

Path: subset of URLs in a domain where it is valid

Domain: domain for which the cookie is valid

Secure: set to '1' to transmit in HTTPS

Reading cookiesReading cookies

$val) {

print $key. " => ". $val. "";

To access a cookie received from a client, use the PHP

$_COOKIE $_COOKIE superglobal array

Each key in the array represents a cookie - the key name is the cookie name.

Using headers ( wrong approach! )

PHP Script using Cookies

"; ?>

Gets an error!: Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/TESTandre/159339/PHP/cookie_with_headers.php:9) in /var/www/html/TESTandre/159339/PHP/cookie_with_headers.php on line 11 (adapted from Stobart & Parsons (2008))

Using headersUsing headers

setcookie() did not run before information

was sent to the browser...

Cookies have to be sent before the heading

elements

Deleting a cookie

Set the cookie with its name only:

setcookie (“mycookie”);

Multiple data items

Use explode() e.g.

$strInfo

"; echo "

Your IP address is: $arrListOfStrings[0]

"; echo "

Client Browser is: $arrListOfStrings[1]

"; echo "

Your OS is: $arrListOfStrings[2]

"; ?>

Where is the cookie stored

Depends on the browser...

e.g., firefox/mozilla under /home/a________

Look for cookies.txt in .mozilla directory

Usually under:

/home/a______/.mozilla/firefox/asdkfljy.default

Cookie is stored only if there is an expiry date

 Otherwise it is deleted when leaving browser

 Persistent only if an expiry date is set