Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > BOOK: Professional PHP 5 ISBN: 978-0-7645-7282-1
| Search | Today's Posts | Mark Forums Read
BOOK: Professional PHP 5 ISBN: 978-0-7645-7282-1
This is the forum to discuss the Wrox book Professional PHP5 by Ed Lecky-Thompson, Heow Eide-Goodman, Steven D. Nowicki, Alec Cove; ISBN: 9780764572821
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional PHP 5 ISBN: 978-0-7645-7282-1 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old August 3rd, 2007, 01:49 AM
Registered User
 
Join Date: Aug 2007
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 15: usersession.phpm database conversion

Can anyone here help me how to convert this code to MySQL (the original code using PostgreSQL). I did some changes to the code myself but it seem like it didn't work. I'm not sure what is the problem and hopefully somebody here willing to share some experience and help me solve my problem.

Code:
<?php
  class UserSession {
    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

    public function __construct() {
      # Connect to database
      $this->dbhandle = mysql_connect("localhost","root","");
      if (!$this->dbhandle)
      {
         die('Could not connect: ' . mysql_error());
      }

      mysql_select_db("prophp5", $this->dbhandle);

      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 \"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 \"user_session\" WHERE (ascii_session_id = '". $this->php_session_id . "') OR (now() - created) > $maxlifetime)");
         # Clean up stray session variables
         $result = mysql_query("DELETE FROM \"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 \"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 \"user\" WHERE username = '$strUsername' AND md5_pw = '$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 \"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 \"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 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 session_variable(session_id, variable_name, variable_value) VALUES(" . $this->native_session_id . ", '$nm', '$strSer')";
      $result = mysql_query($stmt);
    }

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

    private function _session_close_method() {
      mysql_close($this->dbhandle);
      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 \"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 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 \"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 \"user_session\" WHERE ascii_session_id = '$id'");
      return($result);
    }

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


  }
?>
  #2 (permalink)  
Old August 9th, 2007, 04:00 AM
Registered User
 
Join Date: Aug 2007
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

can anyone convert this to MySQL or ODBC?



Similar Threads
Thread Thread Starter Forum Replies Last Post
chapter 15 help kyledonmoyer BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 2 June 30th, 2007 08:06 AM
Chapter 15: usersession.phpm mlange BOOK: Professional PHP 5 ISBN: 978-0-7645-7282-1 12 May 4th, 2007 09:04 AM
Chapter 15 rrlevron BOOK: Beginning ASP 3.0 0 November 17th, 2005 03:38 PM
Chapter 15 moggie BOOK: Beginning ASP 3.0 2 October 19th, 2004 01:30 PM
Chapter 15 help Lorenzo_G BOOK: Beginning ASP 3.0 1 November 27th, 2003 01:00 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.