Created
August 6, 2012 17:49
-
-
Save azrulharis/3277104 to your computer and use it in GitHub Desktop.
Php session finger print
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ProtectPage { | |
public $check_browser = true; | |
public $check_ip_blocks = 0; | |
public $secure_word = 'a39ccdef11305d5999dbccddcf4'; | |
public $regenerate_id = true; | |
public function Open() { | |
$_SESSION['fingerPrint'] = $this->_Fingerprint(); | |
$this->_RegenerateId(); | |
} | |
public function Check() { | |
$this->_RegenerateId(); | |
return (isset($_SESSION['fingerPrint']) && $_SESSION['fingerPrint'] == $this->_Fingerprint()); | |
} | |
public function _Fingerprint() { | |
$fingerprint = $this->secure_word; | |
if ($this->check_browser) { | |
$fingerprint .= $_SERVER['HTTP_USER_AGENT']; | |
} | |
if ($this->check_ip_blocks) { | |
$num_blocks = abs(intval($this->check_ip_blocks)); | |
if ($num_blocks > 4) { | |
$num_blocks = 4; | |
} | |
list($blocks) = explode('.', $_SERVER['REMOTE_ADDR']); | |
for ($i = 0; $i < $num_blocks; $i++) { | |
$fingerprint .= $blocks[$i] . '.'; | |
} | |
} | |
return md5($fingerprint); | |
} | |
private function _RegenerateId() { | |
if ($this->regenerate_id && function_exists('session_regenerate_id')) { | |
if (version_compare(phpversion(), '5.1.0', '>=')) { | |
session_regenerate_id(true); | |
} else { | |
session_regenerate_id(); | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment