<?php // Plug-in 70: Block User By Cookie
/*
* 插件说明:
* 根据cookie值阻止用户访问
* 插件在用户的浏览器里设置一个cookie,利用这个cookie可以判断这个用户是否列在黑名单上。它需要以下参数:
* $action 采取的动作
* $handle 要阻止的用户名。
* $expire cookie的有效时间,单位为妙。
*/
// This is an executable example with additional code supplied
// To obtain just the plug-ins please click on the Download link
$handle = "troll23";
$pass = "itroll4fun";
$name = "Ivor Bigun";
$email = "troll@underbridge.com";
echo PIPHP_BlockUserByCookie(NULL, $handle, NULL);
$result = PIPHP_CreateSession($handle, $pass, $name, $email);
if (!$result) echo "Could not create session.";
else
{
echo 'Session created.<br /><pre>';
echo 'Testing: $_SESSION[\'handle\'] = ' .
$_SESSION['handle'] . '</pre>';
$result = PIPHP_OpenSession();
if (!$result[0]) echo "Could not open session.";
else
{
list($handle, $pass, $name, $email) = $result[1];
echo "Retrieving session variables:<pre>";
echo "Handle: $handle\n";
echo "Pass: $pass\n";
echo "Name: $name\n";
echo "Email: $email</pre>";
}
$result = PIPHP_BlockUserByCookie('block', $handle,
60 * 60 * 24 *365);
if ($result) echo "User '$handle' blocked.";
else echo "Blocking was unsuccessful.";
}
function PIPHP_BlockUserByCookie($action, $handle, $expire)
{
// Plug-in 70: Block User By Cookie
//
// This plug-in either blocks a user or reports on a user's
// block status. It requires the following arguments:
//
// $action: If 'block' set the user's status to blocked,
// otherwise return the user's block status
// $handle: If setting a cookie use this value
// $expire: If setting a cookie use this value
if (strtolower($action) == 'block')
{
if ($_SESSION['handle'] != $handle) return FALSE;
else return PIPHP_manageCookie('set', 'user', $handle,
$expire, '/');
}
return PIPHP_ManageCookie('read', 'user', NULL, NULL, NULL);
}
// The plug-ins below are included here to ensure they
// are available to the main plug-in which relies on them
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;
}
function PIPHP_CreateSession($handle, $pass, $name, $email)
{
// Plug-in 65: Create Session
//
// This plug-in starts a PHP session, assigning the
// four user details as session variables so that no
// further database lookups or logins are required.
// On success it returns TRUE, otherwise FALSE.
// It takes these arguments:
//
// $handle: User handle
// $pass: User password
// $name: User' name
// $email: User's email address
if (!session_start()) return FALSE;
$_SESSION['handle'] = $handle;
$_SESSION['pass'] = $pass;
$_SESSION['name'] = $name;
$_SESSION['email'] = $email;
$_SESSION['ipnum'] = getenv("REMOTE_ADDR");
$_SESSION['agent'] = getenv("HTTP_USER_AGENT");
return TRUE;
}
function PIPHP_OpenSession()
{
// Plug-in 66: Open Session
//
// This plug-in returns the four user variables.
// It doesn't take any parameters. On success it
// returns a two-element array, the first of which
// has the value FALSE, and the second is an array
// of values. On failure (if the session variables
// don't exists, for example), it returns a single
// element array with the value FALSE. An easy way
// to read the return values is with a list()
// statement, like this:
//
// $result = PIPHP_ReadSession();
// list($h, $p, $n, $e) = $result[1];
if (!@session_start()) return array(FALSE);
if (!isset($_SESSION['handle'])) return array(FALSE);
$vars = array();
$vars[] = $_SESSION['handle'];
$vars[] = $_SESSION['pass'];
$vars[] = $_SESSION['name'];
$vars[] = $_SESSION['email'];
return array(TRUE, $vars);
}
?>
插件70:根据cookie值阻止用户访问
最新推荐文章于 2023-02-17 12:20:49 发布