插件70:根据cookie值阻止用户访问

<?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);
}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值