<?php // Plug-in 69: Manage Cookie
/*
* 插件说明:
* 管理Cookie
* 插件可以给Cookie变量设置值,可以读取Cookie变量的值,甚至可以删除cookie变量。
* 需要以下参数:
* $action 对cookie采取的动作:设置值,读取和删除。
* $cookie cookie变量名
* $value cookie变量值
* $expire cookie变量的有效期限,单位为妙
* $path 服务器上cookie路径
*/
// This is an executable example with additional code supplied
// To obtain just the plug-ins please click on the Download link
$cookie = 'Test';
$value = '3.1415927';
$expire = 300;
$path = '/';
$result = PIPHP_ManageCookie('read', $cookie, NULL, NULL, NULL);
if (PIPHP_ManageCookie('set', $cookie, $value, $expire, $path))
echo "Cookie '$cookie' set to value '$value'";
else echo 'Setting of cookie failed';
echo "<br />The read value of '$cookie' is '$result'";
echo '<br />(Reload to read the cookie back).';
function PIPHP_ManageCookie($action, $cookie, $value, $expire,
$path)
{
// Plug-in 69: Manage Cookie
//
// This plug-in provides three ways of interacting with
// cookies. It must be called before any HTML is sent.
// Upon success with a 'set' or 'delete' the plug-in returns
// TRUE. For a successful 'read' it returns the read value.
// On failure it returns FALSE. It requires the following
// arguments:
//
// $action: If 'set' then set $cookie to $value
// If 'read' return the value of $cookie
// If 'delete' delete $cookie
// $cookie: Name of a cookie to set/read/delete
// $value: If setting a cookie use this value: any string
// $expire: If setting a cookie use this value: number
// of seconds before cookie expires, or use
// NULL to let cookie expire at browser session
// end
// $path: The path to the cookie on the server:
// Generally this will be '/'
switch(strtolower($action))
{
case 'set':
if ($expire) $expire += time();
return setcookie($cookie, $value, $expire, $path);
case 'read':
if (isset($_COOKIE[$cookie]))
return $_COOKIE[$cookie];
else return FALSE;
case 'delete':
if (isset($_COOKIE[$cookie]))
return setcookie($cookie, NULL,
time() - 60 * 60 * 24 * 30, NULL);
else return FALSE;
}
return FALSE;
}
?>
插件69:管理cookie
最新推荐文章于 2020-12-26 13:39:24 发布