PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of Bob Dole   Cookie Management Class   example.php   Download  
File: example.php
Role: Example script
Content type: text/plain
Description: Example
Class: Cookie Management Class
Manipulate information stored in cookies
Author: By
Last change: timers
Date: 14 years ago
Size: 1,594 bytes


Class file image Download

    Cookies are sent in the header, so they're not visible or accessble until the next page load
    (ie: refresh if you don't see data)


// Sample data
$array = array('foo'=>'bar','bar'=>'foo');
$string = 'this is a string';

$c = new Cookie();

    Create encrypted cookie with an array
echo '<h3>Encrypted array</h3>';

$start = microtime(true);

$c->setName('Example') // our cookie name
->setValue($array,true) // second parameter, true, encrypts data
->setExpire('+1 hours') // expires in 1 hour
->setPath('/') // cookie path
->setDomain('localhost') // set for localhost
$cookie = $c->getCookie('Example',true);
$cookie = unserialize($cookie);

$bench = sprintf('%.8f',(microtime(true)-$start));

print_r($cookie,true).'<br />'.$bench.' seconds<hr />';

    Destroy Example Cookie
    Note: Domain and path may need to be set if they differ from the defaults,
          but they're already initialized above

    Create cookie with a string that expires when the browser closes (default)
echo '<h3>Regular unencrypted string</h3>';
$start = microtime(true);
setValue($string) // Second param could be set to false here

$cookie = $c->getCookie('Example1');

$bench = sprintf('%.8f',(microtime(true)-$start));

print_r($cookie,true).'<br />'.$bench.' seconds';