time = time() + 61200; $letters = range("a","z"); $key = ""; for($i = 0; $i < 6; $i++) { if(($i == 0) || ($i == 2) || ($i == 5)) { $key .= rand(0,9); } if(($i == 1) || ($i == 3) || ($i == 4)) { $key .= $letters[rand(0,25)]; } } if($timeout == "") { $timeout = 3600; // five minutes } else { $timeout = $timeout; } $this->key = $key; $this->timeout = $timeout; } function register($val) { $this->time = time() + 61200; // if key is not generated run start() if($this->key == "") { $this->start(); } $insert = mysql_query("INSERT INTO sessions (sess_key, val, ip, sec_expire, stamp_expire, access) VALUES ('$this->key', '".addslashes($val)."', '".$_SERVER["REMOTE_ADDR"]."', '$this->timeout', '" . ($this->time + $this->timeout) . "', '$this->time')") or die(mysql_error()); // set the cookie that will store the session key ?> time = time() + 61200; $this->key = addslashes($_COOKIE["sess_key"]); $query = mysql_query("DELETE FROM sessions WHERE sess_key = '$this->key'") or die("query failed - line 86"); $delete = setcookie("sess_key" , $this->key, $this->time-3600); if($query && $delete) { //header("Location login.php"); //exit; } } function expire() { $this->time = time() + 61200; // fetch the last access and expirations from the database $query = mysql_query("SELECT access, sec_expire, stamp_expire FROM sessions WHERE sess_key = '$this->key'") or die("query failed - line 78"); $fetch = mysql_fetch_array($query); $access = $fetch["access"]; $expire = $fetch["sec_expire"]; $timeout = $fetch["stamp_expire"]; // test if session is expired based on defined timeout if(($timeout - $access) <= ($expire - $expire)) { $this->destroy(); } } function read() { $this->time = time() + 61200; // if the cookie doesn't exisit send them back to the login screen. if(!$_COOKIE["sess_key"]) { //header("Location: login.php"); //exit; } // fetch the session key from the cookie. $this->key = addslashes($_COOKIE["sess_key"]); if($this->key != ""){ // fetch the session value $query = mysql_query("SELECT val FROM sessions WHERE sess_key = '$this->key'") or die("Read - ".__LINE__); if(mysql_num_rows($query) == 0) { //header("Location: login.php"); //exit; } $fetch = mysql_fetch_array($query); // store the session value to $sess_val $sess_val = stripslashes($fetch["val"]); // test if session has reached the expiration point $this->expire(); $update = mysql_query("UPDATE sessions SET access = '$this->time' WHERE sess_key = '$this->key'") or die("Read - ".__LINE__); } } function replace($val) { $this->time = time() + 61200; // fetch the user key from cookie $this->key = addslashes($_COOKIE["sess_key"]); // update the database with the new value $query = mysql_query("UPDATE sessions SET val = '$val' WHERE sess_key = '$this->key'") or die("query failed - line ".__LINE__); } } ?>