Advertisement
Guest User

бля

a guest
Mar 14th, 2012
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.41 KB | None | 0 0
  1. <?php
  2.  
  3. class GoodPeopleChecker {
  4.  
  5.     const
  6.         GOOD_COOKIE = 'i_am_good',
  7.         GOOD_COOKIE_VALUE = 'yes',
  8.         GOOD_COOKIE_LIFETIME = 2147483647;
  9.  
  10.     protected
  11.         $is_good = null,
  12.         $ip_addresses;
  13.  
  14.     public function __construct(array $good_peoples_ip_addresses) {
  15.         foreach ($good_peoples_ip_addresses as $ip_address) {
  16.             $this->ip_addresses[ip2long($ip_address)] = true;
  17.         }
  18.     }
  19.  
  20.     public function currentUserIsGood() {
  21.         if (null === $this->is_good) {
  22.             $this->is_good = $this->check();
  23.         }
  24.         return $this->is_good;
  25.     }
  26.  
  27.     protected function check() {
  28.         if (isset($_COOKIE[self::GOOD_COOKIE]) && self::GOOD_COOKIE_VALUE === $_COOKIE[self::GOOD_COOKIE]) {
  29.             return true;
  30.         }
  31.         if (!empty($this->ip_addresses[ip2long($_SERVER['REMOTE_ADDR'])])) {
  32.             $host = isset($_SERVER['HTTP_HOST']) ? '.' . preg_replace('@^www\.@', '', $_SERVER['HTTP_HOST']) : null;
  33.             setcookie(self::GOOD_COOKIE, self::GOOD_COOKIE_VALUE, self::GOOD_COOKIE_LIFETIME, '/', $host);
  34.             return true;
  35.         }
  36.         return false;
  37.     }
  38.  
  39. }
  40.  
  41. ////////////////////////////////////////////////////////////////
  42.  
  43. $GoodPeopleChecker = new GoodPeopleChecker(array(
  44.     '127.0.0.1',
  45.     '192.168.0.1',
  46. ));
  47.  
  48. //...
  49.  
  50. if ($GoodPeopleChecker->currentUserIsGood()) {
  51.     echo 'One I met a boy...';
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement