PHP Classes

File: CookieManager.examples.php

Recommend this page to a friend!
  Classes of F. Scott Gale   Cookie Manager   CookieManager.examples.php   Download  
File: CookieManager.examples.php
Role: Example script
Content type: text/plain
Description: A series of examples to show the use of the CookieManager and associated classes.
Class: Cookie Manager
Manage HTTP request cookies
Author: By
Last change:
Date: 13 years ago
Size: 1,842 bytes
 

Contents

Class file image Download
<?php
// include and instantiate a CookieManager
include_once("CookieManager.class.php");
$cMgr = new CookieManager();

// create two simple cookies (one for prior to PHP 5.2.7 and one for the newest versions)
$cookie1 = new CookiePHP("myCookie", "cookie1value");
$cookie2 = new CookiePHP5("my2ndCookie", "cookie2value");

// add the cookies
$cMgr->add($cookie1);
$cMgr->add($cookie2);

// activate all the present cookies in the manager
// NOTE: by not supplying a $name value it activates all current cookies if they are not already activated
$cMgr->activate();

// create another cookie, let the manager determine the object type to use based on PHP version
$cMgr->create("anotherCookie", "cookie3value", time() + 3600, "", ".example.com", false, true);

// deactivate a specific cookie
// NOTE: by supplying a cookie's name it will deactivate only that cookie
$cMgr->deactivate("my2ndCookie");

// make sure it's deactivated
if (!$cMgr->activated("my2ndCookie"))
{
   
// NOTE: you probably wouldn't want to do this with other cookie actions being performed afterwards
   
echo "my2ndCookie deactivated successfully";
}

// make sure the cookie exists
if ($cMgr->exists("anotherCookie"))
{
   
// activate it
   
$cMgr->activate("anotherCookie");
   
   
// verify
   
if ($cMgr->activated("anotherCookie"))
    {
       
// NOTE: you probably wouldn't want to do this with other cookie actions being performed afterwards
       
echo "anotherCookie was successfully activated";
    }
}

// completely remove a cookie
$cMgr->remove("my2ndCookie");

// get direct access to a specific cookie
$aCookie = $cMgr->getCookie("anotherCookie");
// verify it's not activated
if (!$aCookie->status())
{
   
// change its value and activate it
   
$aCookie->Value = "changedValue";
   
$aCookie->Secure = true;
   
$aCookie->activate();
}
?>