View Single Post
  #10 (permalink)  
Old February 27th, 2007, 10:27 AM
hammet hammet is offline
Registered User
 
Join Date: Feb 2007
Location: Donetsk, , Ukraine.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to hammet
Default

Can anybody helps me...?
I rewrite this class for MySQL. But it didn't works.

Fatal error: Call to private method Auth::_session_write_method() from context '' in Unknown on line 0

Fatal error: Call to private method Auth::_session_close_method() from context '' in Unknown on line 0


class code here:
Code:
<?php
  error_reporting(E_ALL ^ E_NOTICE);

  include "config.php";

  class Auth {

    private $php_session_id;
    private $native_session_id;
    private $dbhandle;
    private $logged_in;
    private $user_id;
    private $session_timeout = 600;      # 10 minute inactivity timeout
    private $session_lifespan = 3600;    # 1 hour session duration

    private $dbhost;
    private $dblogin;
    private $dbpass;
    private $dbname;

    public function __construct() {

      # Connect to database
      $this->dbhost  = DB_HOST; 
      $this->dblogin = DB_USER;
      $this->dbpass  = DB_PASS;
      $this->dbname  = DB_NAME;
      $link = @mysql_connect($this->dbhost, $this->dblogin, $this->dbpass);
      @mysql_select_db($this->dbname, $link);

      # Set up the handler
      session_set_save_handler(
          array(&$this, '_session_open_method'), 
          array(&$this, '_session_close_method'), 
          array(&$this, '_session_read_method'), 
          array(&$this, '_session_write_method'), 
          array(&$this, '_session_destroy_method'), 
          array(&$this, '_session_gc_method')
      );
      # Check the cookie passed - if one is - if it looks wrong we'll scrub it right away      
      $strUserAgent = $GLOBALS["HTTP_USER_AGENT"];
      if ($_COOKIE["PHPSESSID"]) {
       # Security and age check
       $this->php_session_id = $_COOKIE["PHPSESSID"];
       $stmt = "select id from `kc_user_session` where ascii_session_id = '" . $this->php_session_id . "' AND ((now() - created) < ' " . $this->session_lifespan . " seconds') AND user_agent='" . $strUserAgent . "' AND ((now() - last_impression) <= '".$this->session_timeout." seconds' OR last_impression IS NULL)";
       $result = mysql_query($stmt);
       if (mysql_num_rows($result)==0) {
         # Set failed flag
          $failed = 1;
         # Delete from database - we do garbage cleanup at the same time
         $result = mysql_query("DELETE FROM `kc_user_session` WHERE (ascii_session_id = '". $this->php_session_id . "') OR (now() - created) > $maxlifetime)");
         # Clean up stray session variables
         $result = mysql_query("DELETE FROM `kc_session_variable` WHERE session_id NOT IN (SELECT id FROM `user_session`)");
         # Get rid of this one... this will force PHP to give us another
         unset($_COOKIE["PHPSESSID"]);
       };  
      };
      # Set the life time for the cookie
      session_set_cookie_params($this->session_lifespan);
      # Call the session_start method to get things started
      session_start();
    }

    public function Impress() {
      if ($this->native_session_id) {
        $result = mysql_query("UPDATE `kc_user_session` SET last_impression = now() WHERE id = " . $this->native_session_id);
      }
    }

    public function IsLoggedIn() {
      return($this->logged_in);
    }

    public function GetUserID() {
      if ($this->logged_in) {
        return($this->user_id);
      } else {
        return(false);
      };
    }

    public function GetUserObject() {
      if ($this->logged_in) {
        if (class_exists("user")) {
          $objUser = new User($this->user_id);
          return($objUser);
        } else {
          return(false);
        };
      };
    }

    public function GetSessionIdentifier() {
      return($this->php_session_id);
    }

    public function Login($strUsername, $strPlainPassword) {
      $strMD5Password = md5($strPlainPassword);
      $stmt = "select id FROM `kc_users` WHERE login = '$strUsername' AND password = '$strMD5Password'";
      $result = mysql_query($stmt);
      if (mysql_num_rows($result)>0) {
        $row = mysql_fetch_array($result);
        $this->user_id = $row["id"];
        $this->logged_in = true;
        $result = mysql_query("UPDATE `kc_user_session` SET logged_in = true, user_id = " . $this->user_id . " WHERE id = " . $this->native_session_id);
        return true;
      } 
      else {
        return false;
      }
    }  

    public function LogOut() {
      if ($this->logged_in == true) {
        $result = mysql_query("UPDATE `kc_user_session` SET logged_in = false, user_id = 0 WHERE id = " . $this->native_session_id);
        $this->logged_in = false;
        $this->user_id = 0;
        return(true);
      } else {
        return(false);
      }
    }

    public function __get($nm) {
      $result = mysql_query("SELECT variable_value FROM `kc_session_variable` WHERE session_id = " . $this->native_session_id . " AND variable_name = '" . $nm . "'");
      if (mysql_num_rows($result)>0) {
        $row = mysql_fetch_array($result);
        return(unserialize($row["variable_value"]));
      } else {
        return(false);
      }
    }


    public function __set($nm, $val) {
      $strSer = serialize($val);
      $stmt = "INSERT INTO kc_session_variable(session_id, variable_name, variable_value) VALUES(" . $this->native_session_id . ", '$nm', '$strSer')";
      $result = mysql_query($stmt);
    }

    public function _session_open_method($save_path, $session_name) {
      # Do nothing
      return(true);
    }

    private function _session_close_method() {
      @mysql_close($this->link);
      return(true);
    }

    private function _session_read_method($id) {
      # We use this to determine whether or not our session actually exists.
      $strUserAgent = $GLOBALS["HTTP_USER_AGENT"];
      $this->php_session_id = $id;
      # Set failed flag to 1 for now
      $failed = 1;
      # See if this exists in the database or not.
      $result = mysql_query("select id, logged_in, user_id from `kc_user_session` where ascii_session_id = '$id'");
      if (mysql_num_rows($result)>0) {
       $row = mysql_fetch_array($result);
       $this->native_session_id = $row["id"];
       if ($row["logged_in"]=="t") {
         $this->logged_in = true;
         $this->user_id = $row["user_id"];
       } else {
         $this->logged_in = false;
       };
      } else {
        $this->logged_in = false;
        # We need to create an entry in the database
        $result = mysql_query("INSERT INTO kc_user_session (ascii_session_id, logged_in, user_id, created, user_agent) VALUES ('$id','f',0,now(),'$strUserAgent')");
        # Now get the true ID
        $result = mysql_query("select id from `kc_user_session` where ascii_session_id = '$id'");
        $row = mysql_fetch_array($result);
        $this->native_session_id = $row["id"];
      };
      # Just return empty string
      return("");
    }

    private function _session_write_method($id, $sess_data) {
      return(true);
    }

    private function _session_destroy_method($id) {
      $result = mysql_query("DELETE FROM `kc_user_session` WHERE ascii_session_id = '$id'");
      return($result);
    }

    private function _session_gc_method($maxlifetime) {
      return(true);
    }


  }
?>
...Thanks!