Advertisement
Guest User

Untitled

a guest
Jul 19th, 2017
476
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.42 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.   SecureSession class
  5.   Written by Vagharshak Tozalakyan <vagh@armdex.com>
  6.   Released under GNU Public License
  7. */
  8.  
  9. class SecureSession
  10. {
  11.     // Include browser name in fingerprint?
  12.     var $check_browser = true;
  13.  
  14.     // How many numbers from IP use in fingerprint?
  15.     var $check_ip_blocks = 0;
  16.  
  17.     // Control word - any word you want.
  18.     var $secure_word = 'SECURESTAFF';
  19.  
  20.     // Regenerate session ID to prevent fixation attacks?
  21.     var $regenerate_id = true;
  22.  
  23.     // Call this when init session.
  24.     function Open()
  25.     {
  26.         $_SESSION['ss_fprint'] = $this->_Fingerprint();
  27.         $this->_RegenerateId();
  28.     }
  29.  
  30.     // Call this to check session.
  31.     function Check()
  32.     {
  33.         $this->_RegenerateId();
  34.         return (isset($_SESSION['ss_fprint']) && $_SESSION['ss_fprint'] == $this->_Fingerprint());
  35.     }
  36.  
  37.     // Internal function. Returns MD5 from fingerprint.
  38.     function _Fingerprint()
  39.     {
  40.         $fingerprint = $this->secure_word;
  41.         if ($this->check_browser) {
  42.             $fingerprint .= $_SERVER['HTTP_USER_AGENT'];
  43.         }
  44.         if ($this->check_ip_blocks) {
  45.             $num_blocks = abs(intval($this->check_ip_blocks));
  46.             if ($num_blocks > 4) {
  47.                 $num_blocks = 4;
  48.             }
  49.             $blocks = explode('.', $_SERVER['REMOTE_ADDR']);
  50.             for ($i = 0; $i < $num_blocks; $i++) {
  51.                 $fingerprint .= $blocks[$i] . '.';
  52.             }
  53.         }
  54.         return md5($fingerprint);
  55.     }
  56.  
  57.     // Internal function. Regenerates session ID if possible.
  58.     function _RegenerateId()
  59.     {
  60.         if ($this->regenerate_id && function_exists('session_regenerate_id')) {
  61.             if (version_compare(phpversion(), '5.1.0', '>=')) {
  62.                 session_regenerate_id(true);
  63.             } else {
  64.                 session_regenerate_id();
  65.             }
  66.         }
  67.     }
  68. }
  69.  
  70. ?>
  71.  
  72. =============================
  73. example index.php
  74. <?php
  75.   session_start();
  76.   require_once '../securesession.class.php';
  77.   $ss = new SecureSession();
  78.   $ss->check_browser = true;
  79.   $ss->check_ip_blocks = 2;
  80.   $ss->secure_word = 'SALT_';
  81.   $ss->regenerate_id = true;
  82.   if (!$ss->Check() || !isset($_SESSION['logged_in']) || !$_SESSION['logged_in'])
  83.   {
  84.     header('Location: login.php');
  85.     die();
  86.   }
  87. ?>
  88. <html>
  89. <head>
  90. <title>SecureSession Sample</title>
  91. </head>
  92. <body>
  93. You are successfully logged in!
  94. </body>
  95. </html>
  96.  
  97. =======================
  98. example login script:
  99. <?php
  100.   session_start();
  101.   require_once '../securesession.class.php';
  102.   $error = '';
  103.   if (isset($_POST['uname']))
  104.   {
  105.     $uname = $_POST['uname'];
  106.     $passwd = $_POST['passwd'];
  107.     if ($uname == 'User' && $passwd == 'password')
  108.     {
  109.       $ss = new SecureSession();
  110.       $ss->check_browser = true;
  111.       $ss->check_ip_blocks = 2;
  112.       $ss->secure_word = 'SALT_';
  113.       $ss->regenerate_id = true;
  114.       $ss->Open();
  115.       $_SESSION['logged_in'] = true;
  116.       header('Location: index.php');
  117.       die();
  118.     }
  119.     else
  120.     {
  121.       $error = 'Incorrect username or password.';
  122.     }
  123.   }
  124. ?>
  125.  
  126. <html>
  127. <head>
  128. <title>SecureSession Sample</title>
  129. </head>
  130. <body>
  131. <?php
  132.   if (!empty($error))
  133.   {
  134.     echo $error;
  135.   }
  136. ?>
  137. <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  138. Username: <input type="text" name="uname" />
  139. Password: <input type="password" name="passwd" />
  140. <input type="submit" value="Log In" />
  141. </form>
  142. </body>
  143. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement