Tak, więc pod spodem wklejam troche zmodyfikowana klase sessji, nie chce mi sie jej konczyc, a moze ktos bedzie mial ochote dopisac z 2 linijki to dlaczego nie? smile.gif ja bede dolaczal kod do 1 posta, czyli tego. No to jedziemy.
  1. <?php
  2.  
  3. /**
  4.  *  @version 1.0
  5.  *  @author Przemek Czekaj
  6.  *  @licenses GNU Affero General Public License version 3.0
  7.  *
  8.  *  secureSession class, based on SecureSession class by Vagharshak Tozalakyan
  9.  *  Copyright (C) 2009 Przemek Czekaj <xcojack@gmail.com>
  10.  *
  11.  *  This program is free software: you can redistribute it and/or modify
  12.  *  it under the terms of the GNU Affero General Public License as
  13.  *  published by the Free Software Foundation, either version 3 of the
  14.  *  License, or (at your option) any later version.
  15.  *
  16.  *  This program is distributed in the hope that it will be useful,
  17.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.  *  GNU Affero General Public License for more details.
  20.  *
  21.  *  You should have received a copy of the GNU Affero General Public License
  22.  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  23.  */
  24.  
  25. class secureSession
  26. {
  27.    // We'll save config in the db
  28.    private $db_config = true;
  29.  
  30.    // Include browser name in fingerprint?
  31.    private $check_browser = true;
  32.  
  33.    // How many numbers from IP use in fingerprint?
  34.    private $check_ip_blocks = 2;
  35.  
  36.    // Control word - any word you want.
  37.    private $secure_word = 'GzuH3sLaURRCsuCd';
  38.  
  39.    // Regenerate session ID to prevent fixation attacks?
  40.    private $regenerate_id = true;
  41.    
  42.    // Adding user id to fingerprint.
  43.    public $user_id = '';
  44.  
  45.    // Check auto start session, we have to.
  46.    private $session_auto_start;
  47.  
  48.    // Session time out in seconds.
  49.    private $session_timeout = 1800;
  50.  
  51.    // Session life time
  52.    private $session_lifetime = 86400;
  53.  
  54.    // Session name, remember the session name can't consist of digits only, at least one letter must be present. Otherwise a new session id is generated every time.
  55.    private $sessionName = 'Testing';
  56.  
  57.    // Handle of database connection, we use PDO for connect, executing queries etc.
  58.    private $db_handle;
  59.  
  60.    public function __construct($dbhandle)
  61.    {
  62.      // Set handle for database connect
  63.      $this->db_handle = $dbhandle;
  64.  
  65.      // Auto call to set variables from db when we want to save config in the db
  66.      if($this->db_config)
  67.        $this->getParams();
  68.  
  69.      $this->session_auto_start = ini_get('session.auto_start');
  70.      // We have to check, the auto start session, if is we have to close session, becouse session_set_save_handler must be executed first than session_start();
  71.      if($this->session_auto_start == 'On')
  72.        session_write_close();
  73.  
  74.      // Session handling mechanism
  75.      (
  76.        array(&$this, '_session_open_method'),
  77.        array(&$this, '_session_close_method'),
  78.        array(&$this, '_session_read_method'),
  79.        array(&$this, '_session_write_method'),
  80.        array(&$this, '_session_destroy_method'),
  81.        array(&$this, '_session_gc_method')
  82.      );
  83.  
  84.      // Set life time of session cookie$this->db_handle = $dbhandle;
  85.      session_set_cookie_params($this->session_lifetime);
  86.      
  87.      // Set session name
  88.      session_name($this->sessionName);
  89.  
  90.      // Start session
  91.      session_start();
  92.  
  93.    }
  94.  
  95.    private function getParams()
  96.    {
  97.        $config = $this->db_handle->prepare("SELECT * FROM session_config");
  98.        $config->execute();
  99.      if($config)
  100.        throw new Exception ("Error with geting data from table session_config.");
  101.      else
  102.      {
  103.        $config->fetchObject();
  104.        $this->setSessionName($config->sessionName);
  105.        $this->setCheckBrowser($config->check_browser);
  106.        $this->setCheckIpBlocks($config->check_ip_blocks);
  107.        $this->setSecureWord($config->secure_word);
  108.        $this->setRegenerateId($config->regenerate_id);
  109.    }
  110.  
  111.    // Set session name, alphabetic
  112.    private function setSessionName($sn)
  113.    {
  114.      $this->sessionName = $sn;
  115.    }
  116.  
  117.    // Set browser checking in fingerprint, boolean true or false
  118.    public function setCheckBrowser($bw)
  119.    {
  120.      $this->check_browser = $bw;
  121.    }
  122.  
  123.    // Set how many ip block will be checked, int max 4
  124.    public function setCheckIpBlocks($cib)
  125.    {
  126.      $this->check_ip_blocks = $cib;
  127.    }
  128.  
  129.    // Set secure word for fingerprint
  130.    public function setSecureWord($sw)
  131.    {
  132.      $this->secure_word = $sw;
  133.    }
  134.  
  135.    // Set regenerate id, boolean true or false
  136.    public function setRegenerateId($ri)
  137.    {
  138.      $this->regenerate_id = $ri;
  139.    }
  140.  
  141.    // Call this when init session.
  142.    public function open()
  143.    {
  144.        $_SESSION['ss_fprint'] = $this->setFingerprint();
  145.        $this->regenerateId();
  146.        $this->setCookie();
  147.    }
  148.  
  149.    private function _session_open_method($save_path, $session_name)
  150.    {
  151.      return(true);
  152.    }
  153.  
  154.    // Let php to read the session from db!
  155.    private function _session_read_method($id)
  156.    {
  157.      
  158.    }
  159.  
  160.    private function _session_close_method()
  161.    {
  162.      return(true);
  163.    }
  164.  
  165.    private function _session_gc_method($maxlifetime)
  166.    {
  167.      return(true);
  168.    }
  169.  
  170.    private function _session_destroy_method($id)
  171.    {
  172.        $delete = $this->db_handle->prepare("DELETE FROM session WHERE session_fprint = :ss_fprint");
  173.        $delete->bindParam(':ss_fprint', $_SESSION['ss_fprint']);
  174.        $delete->execute()
  175.        if(!$delete)
  176.          throw new Exception ("There is an error corupted in query to delete session from db");
  177.        else
  178.        {
  179.          $_SESSION = array();
  180.          session_destroy();
  181.        }
  182.    }
  183.  
  184.    // Call this to check session.
  185.    public function checkSession($db_session)
  186.    {
  187.        $this->regenerateId();
  188.        if(isset($_SESSION['ss_fprint']) && ($this->setFingerprint() == $_SESSION['ss_fprint'] == $db_session))
  189.          return(true);
  190.        else
  191.          $this->_session_destroy_method();
  192.          
  193.    }
  194.  
  195.    private function setCookie()
  196.    {
  197.        setcookie('session_id',$_SESSION['ss_fprint'],(time()+$this->session_timeout));
  198.    }
  199.    
  200.    // Internal function. Returns MD5 from fingerprint.
  201.    final private function setFingerprint()
  202.    {
  203.        $fingerprint = $this->secure_word;
  204.        if ($this->check_browser) {
  205.            $fingerprint .= $_SERVER['HTTP_USER_AGENT'];
  206.        }
  207.        if ($this->check_ip_blocks) {
  208.            $num_blocks = abs(intval($this->check_ip_blocks));
  209.            if ($num_blocks > 4) {
  210.                $nxum_blocks = 4;
  211.            }
  212.            $blocks = explode('.', $_SERVER['REMOTE_ADDR']);
  213.            for ($i = 0; $i < $num_blocks; $i++) {
  214.                $fingerprint .= $blocks[$i] . '.';
  215.            }
  216.            $fingerprint .= $this->user_id;
  217.        }
  218.        return md5($fingerprint);
  219.    }
  220.  
  221.    // Internal function. Regenerates session ID if possible.
  222.    final private function regenerateId()
  223.    {
  224.        if ($this->regenerate_id && function_exists('session_regenerate_id')) {
  225.            if (version_compare('5.1.0', phpversion(), '>=')) {
  226.                session_regenerate_id(true);
  227.            } else {
  228.                session_regenerate_id();
  229.            }
  230.        }
  231.    }
  232. }
  233.  
  234. ?>