插件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  = "[email protected]";

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

?>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章