Jump to content

[SOLVED] need help with session handler


aruns

Recommended Posts

<?php
class SessionHandler{

var $_conn;

public $_server;

public $_user;

public $_password;

public $_db;

function SessionHandler($_server = "", $_user = "", $_password = "", $_db = ""){
	$this->_server = $_server;
	$this->_user = $_user;
	$this->_password = $_password;
	$this->_db = $_db;
}

function open(){
	$this->_conn = mysql_connect($this->_server, $this->_user, $this->_password)or die(mysql_error());
	if($this->_conn){
		return mysql_select_db($this->_db, $this->_conn);
	}
}

function close(){
	return mysql_close($this->_conn);
}

function read($id){
	$sql = mysql_query("SELECT session_data FROM sessions WHERE session = '$id'", $this->_conn)or die(mysql_error());
	if(mysql_num_rows($sql) > 0){
		$data = mysql_fetch_array($sql);
		return stripslashes($data[session_data]);
	}else{
		return "";
	}
}

function write($id, $data){
	$clean_data = addslashes($data);
	$expires = time() + ini_get("session.gc_maxlifetime");
		$query = "INSERT INTO sessions(session, session_data, session_expires)VALUES('$id', '$data', '$expires')";
		$sql = mysql_query($query);
		if(!$sql){
			$query = "UPDATE sessions SET session = '$id', session_data = '$data' WHERE session_expires > '" . time() . "'";
			$sql = mysql_query($query);
		}
			return $sql;
}

function destroy($id){
	$query = "DELETE FROM sessions WHERE session = '$id'";
	return mysql_query($query);
}

function gc($max){
	$query = "DELETE FROM sessions WHERE session_expires < '" . time() . "'";
	return mysql_query($query);
}

function set_handler(){
	ini_set('session.save_handler', 'user');
	session_set_save_handler($this->open(), $this->close(), $this->read(), $this->write(), $this->destroy(), $this->gc());
	if(session_id() == ""){
		session_start();
		print session_id();
	}

}
}

$obj = new SessionHandler();
$obj->_server = "localhost";
$obj->_user = "root";
$obj->_password = "123456";
$obj->_db = "fairy";
$obj->set_handler();
?>

Link to comment
https://forums.phpfreaks.com/topic/162871-solved-need-help-with-session-handler/
Share on other sites

I am Getting this Error :

 

Warning: Missing argument 1 for SessionHandler::read(), called in C:\xampp\htdocs\packs\test.php on line 66 and defined in C:\xampp\htdocs\packs\test.php on line 32

 

Warning: mysql_query(): 2 is not a valid MySQL-Link resource in C:\xampp\htdocs\packs\test.php on line 33

Just a few things -

1. Why did you create a constructor if you don't bother to use it?

2. Loving the public $_password part. That is nice! <_<

 

When you wrote this -

session_set_save_handler($this->open(), $this->close(), $this->read(), $this->write(), $this->destroy(), $this->gc());

 

you're calling the read method without an argument. You defined the read method to take an $id parameter, but you didn't pass it one so it's complaining about that.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.