PHP Classes
elePHPant
Icontem

File: example.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  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: 5 years ago
Size: 1,594 bytes
 

Contents

Class file image Download
<?php

/*
    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)
*/


require('cookie.class.php');

// 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
 
->createCookie();
$cookie = $c->getCookie('Example',true);
$cookie = unserialize($cookie);

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

echo
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
*/
//$c->destroyCookie('Example');


/*
    Create cookie with a string that expires when the browser closes (default)
*/
echo '<h3>Regular unencrypted string</h3>';
$start = microtime(true);
$c->setName('Example1')
  ->
setValue($string) // Second param could be set to false here
 
->setExpire(0)
  ->
setPath('/')
  ->
setDomain('localhost')
  ->
createCookie();

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

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

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