Cookies

Derek Bridge

Department of Computer Science,
University College Cork

Cookies

Aims:

State management

Ways to achieve state management

Cookies for state management

Cookies example

Persistent cookies and in-memory cookies

Cookies in PHP: sending them

Cookies in PHP: sending them

(*unless PHP's output buffering is enabled, which it is not in our intallation)

Cookies in PHP: accessing them

counter.php: simple example

Counting the number of times a page has been accessed by a client

<?php 
 if (! isset($_COOKIE['numvisits'])) 
 {
    $counter = 1;
 }
 else 
 {
    $counter = $_COOKIE['numvisits'] + 1;
 }
 setcookie("numvisits", $counter, time()+86400*365*5);
?>
...
<php
 echo "

Welcome! (Visit number: {$counter})

"; ?>

Some of the issues with cookies