User Tools

Site Tools


libraries:cookiehandler:practicalexamples

Practical examples


Setting of a simple cookie with a lifespan of 7 days.

require_once 'CookieHandler/CookieHandler.php'
 
$Cookie = new CookieHandler("Supercookie");
 
$Cookie->SetValidity(7);
$Cookie->WriteTextContent("Cookie text");

The simpler way...

… to save one line of code.

require_once 'CookieHandler/CookieHandler.php'
 
$Cookie = new CookieHandler("Supercookie");
 
$Cookie->WriteTextContent("Cookie text", 7);

You can read data stored in a cookie the following way.

require_once 'CookieHandler/CookieHandler.php'
 
$Cookie = new CookieHandler("Supercookie");
 
$CookieText = $Cookie->ReadTextContent();  // "Cookie-Text"

What about this JSON method?

Sometimes it could be helpful to store more that simple text. e.g. the content of a PHP array as content string. This could be done the following way for a cookie with a lifespan of 10 days and 12 hours.

require_once 'CookieHandler/CookieHandler.php'
 
$Cookie = new CookieHandler("UserCookie");
$UserArray = array ( 
   "name"     => "Mr. Smith",
   "passwort" => "secret");
 
// Writing the object content als JSON string  
$Cookie->WriteJsonContent($UserArray, 10, 12);
 
// [...]
 
// Reading the JSON string into an array
$NewArray = $Cookie->ReadJsonContent();

If a cookie should be removed completly, it can be deleted. The cookieis removed from the storage and the current browser session.

require_once 'CookieHandler/CookieHandler.php'
 
$Cookie = new CookieHandler("Supercookie");
 
$Cookie->DeleteCookie();
libraries/cookiehandler/practicalexamples.txt · Last modified: 2022/12/14 21:36 by michael.pohl