Sådan benyttes komponenten Cookie klassen
Først skal du inkludere den fil der beskriver komponenten, som en klasse fil
<? require_once(HTML_PACKAGE_PATH.'/Cookie.php'); ?>
Dernæst kan du enten benytte komponenten som et taglib (statiske metoder):
<? Cookie::display($param1, $param2, $param3, ...); ?>
eller du kan lave en instance af komponenten og benytte metoderne direkte:
<? $object = new Cookie($param1, $param2, $param3, ...); print $object->getHtml(); ?>
Sådan vises komponenten Cookie klassen
Den fulde PHP kildekode for Cookie klassen
<?php/** * @package util * @filesource * @see HTML_UTIL_COMPONENT_PATH.'/Cookie.php' * @copyright (c) http://Finn-Rasmussen.com * @license http://Finn-Rasmussen.com/license/ myPHP License conditions * @author http://Finn-Rasmussen.com * @version 1.11 * @since 27-nov-2009 *//** * The required files */require_once(HTML_BASIC_UTIL_PATH.'/Message.php');require_once(HTML_UTIL_COMPONENT_PATH.'/Server.php');if (defined('HTML_LOG_UTIL_PATH')) { require_once(HTML_LOG_UTIL_PATH.'/Log.php');}/** * Set/get or delete a cookie * <code> * Usage: * $cookie = new Cookie($name, $value, $time, $path, $domain, $secure); * $rc = $cookie->setCookie(); * $value = $cookie->getCookie(); * $rc = $cookie->deleteCookie(); * Or * $rc = Cookie::set($name, $value, $time, $path, $domain, $secure); * $value = Cookie::get($name); * $rc = Cookie::delete($name); * </code> * @package util */class Cookie { /** * @var String $name The name of the cookie to get/set */ private $name = ''; /** * @var String $value The value of the cookie */ private $value = ''; /** * @var String $time The time in sec. */ private $time = ''; /** * @var String $path The root path of columns */ private $path = ''; /** * @var String $domain The domain */ private $domain = ''; /** * @var String $secure The secure */ private $secure = ''; /** * Constructor * @param String $name The name of the cookie * @param String $value The value, if any * @param String $time The time * @param String $path The path * @param String $domain The domain * @param String $secure The secure */ function __construct($name='', $value='', $time='', $path='', $domain='', $secure='') { $this->name = $name != '' ? $name : COOKIE_NAME; $this->value = $value != '' ? $value : ''; $this->time = $time != '' ? $time : COOKIE_TIME; $this->path = $path != '' ? $path : COOKIE_PATH; $this->domain = $domain != '' ? $domain : COOKIE_DOMAIN; $this->secure = $secure != '' ? $secure : 0; } /** * Set the cookie information * <code> * Usage: * $cookie = new Cookie($name, $value, $time, $path, $domain, $secure); * $rc = $cookie->setCookie($filename, $lineno); * </code> * @param String $filename The File name * @param String $lineno The Line number * @return int The status */ function setCookie($filename='', $lineno='') { $rc = false; if (headers_sent($filename, $lineno)) { $msg = "Cookie->setCookie() detected headers_sent, see $filename, $lineno<br />\r\n"; Message::add($msg, __FILE__, __LINE__); } else { $rc = setcookie($this->name, $this->value, $this->time, $this->path, $this->domain, $this->secure); // Set cookie info } if (defined('DEBUG_LEVEL') && DEBUG_LEVEL & DEBUG_LEVEL_SHOW_INFO) { $msg = "Cookie->setCookie() $this->name, $this->value, $this->time, $this->path, $this->domain, $this->secure"; Log::debug($msg, __FILE__, __LINE__); } return $rc; } /** * Delete the cookie information * <code> * Usage: * $cookie = new Cookie($name); * $rc = $cookie->deleteCookie($filename, $lineno); * </code> * @param String $filename The File name * @param String $lineno The Line number * @return int The status */ function deleteCookie($filename='', $lineno='') { $rc = false; if (headers_sent($filename, $lineno)) { $msg = "Cookie->deleteCookie() detected headers_sent, see $filename, $lineno<br />\r\n"; Message::add($msg, __FILE__, __LINE__); } else { $rc = setcookie($this->name,'',(time()-2592000),'/','',0); // Delete the cookie } if (defined('DEBUG_LEVEL') && DEBUG_LEVEL & DEBUG_LEVEL_SHOW_INFO) { $msg = "Cookie->deleteCookie() $this->name"; Log::debug($msg, __FILE__, __LINE__); } return $rc; } /** * Get the cookie value from the specified cookie name * <code> * $cookie = new Cookie($name); * $value = $cookie->getCookie(); * </code> * @param String $name The name of the cookie value to get * @return String The cookie value or "" if not found */ function getCookie() { $value = ''; // Nothing found if (!empty($_COOKIE[$this->name])) { $value = $_COOKIE[$this->name]; } if (defined('DEBUG_LEVEL') && DEBUG_LEVEL & DEBUG_LEVEL_SHOW_INFO) { $msg = "Cookie->getCookie() $this->name $value"; Log::debug($msg, __FILE__, __LINE__); } return $value; } /** * Delete the cookie with the specified cookie name * <code> * $rc = Cookie::delete($name, $filename, $lineno); * </code> * @static * @param String $name The name of the cookie value to delete * @param String $filename The File name * @param String $lineno The Line number * @return int The status */ public static function delete($name, $filename='', $lineno='') { $cookie = new Cookie($name); return $cookie->deleteCookie($filename, $lineno); } /** * Set the cookie information * <code> * Usage: * $rc = Cookie::set($name, $value, $time, $path, $domain, $secure, $filename, $lineno); * </code> * @static * @param String $name The name of the cookie * @param String $value The value, if any * @param String $time The time * @param String $path The path * @param String $domain The domain * @param String $secure The secure * @param String $filename The File name * @param String $lineno The Line number * @return int The status */ public static function set($name='', $value='', $time='', $path='', $domain='', $secure='', $filename='', $lineno='') { $cookie = new Cookie($name, $value, $time, $path, $domain, $secure); return $cookie->setCookie($filename, $lineno); } /** * Get the cookie value from the specified cookie name * <code> * $value = Cookie::get($name); * </code> * @param String $name The name of the cookie value to get * @return String The cookie value or "" if not found */ public static function get($name) { $cookie = new Cookie($name); return $cookie->getCookie(); }}?>
Den fulde HTML kildekode for Cookie klassen
<? Der er ikke fundet noget ?>
Her er 'klasse metoderne' for Cookie klassen:
Her er 'objekt variable' for Cookie klassen: