Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class GoodPeopleChecker {
- const
- GOOD_COOKIE = 'i_am_good',
- GOOD_COOKIE_VALUE = 'yes',
- GOOD_COOKIE_LIFETIME = 2147483647;
- protected
- $is_good = null,
- $ip_addresses;
- public function __construct(array $good_peoples_ip_addresses) {
- foreach ($good_peoples_ip_addresses as $ip_address) {
- $this->ip_addresses[ip2long($ip_address)] = true;
- }
- }
- public function currentUserIsGood() {
- if (null === $this->is_good) {
- $this->is_good = $this->check();
- }
- return $this->is_good;
- }
- protected function check() {
- if (isset($_COOKIE[self::GOOD_COOKIE]) && self::GOOD_COOKIE_VALUE === $_COOKIE[self::GOOD_COOKIE]) {
- return true;
- }
- if (!empty($this->ip_addresses[ip2long($_SERVER['REMOTE_ADDR'])])) {
- $host = isset($_SERVER['HTTP_HOST']) ? '.' . preg_replace('@^www\.@', '', $_SERVER['HTTP_HOST']) : null;
- setcookie(self::GOOD_COOKIE, self::GOOD_COOKIE_VALUE, self::GOOD_COOKIE_LIFETIME, '/', $host);
- return true;
- }
- return false;
- }
- }
- ////////////////////////////////////////////////////////////////
- $GoodPeopleChecker = new GoodPeopleChecker(array(
- '127.0.0.1',
- '192.168.0.1',
- ));
- //...
- if ($GoodPeopleChecker->currentUserIsGood()) {
- echo 'One I met a boy...';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement