patternphpMinor
Custom session class
Viewed 0 times
customclasssession
Problem
```
db = $pdo;
$this->table_name = (is_null($table_name)) ? $this->table_name : $table_name;
$this->session_name = (is_null($session_name)) ? $this->session_name : $session_name;
$this->session_litetime = (is_null($session_lifetime)) ? $this->session_litetime : $session_lifetime;
session_set_save_handler(
[$this, 'open'], [$this, 'close'], [$this, 'read'], [$this, 'write'], [$this, 'destroy'], [$this, 'gc']
);
register_shutdown_function('session_write_close');
ini_set('session_auto_start', 0);
ini_set('session.cookie_httponly',1);
ini_set('session.use_only_cookies',1);
ini_set('session.gc_probability', 25);
ini_set('session.gc_divisor', 100);
ini_set('session.gc_maxlifetime', $this->session_litetime);
session_name($this->session_name);
session_start();
$this->regenerate_id();
}
private function __clone(){}
public function open($save_path, $session_name){
return true;
}
public function close(){
$this->gc($this->session_litetime);
return true;
}
public function read($session_id){
$session = $this->fetch_session($session_id);
return ($session === false) ? false : $session['data'];
}
public function write($session_id, $session_data){
$session = $this->fetch_session($session_id);
if($session === false)
$stmp = $this->db->prepare('INSERT INTO '.$this->get_table().' (id, data, unixtime) VALUES (:id, :data, :time)');
else
$stmp = $this->db->prepare('UPDATE '.$this->get_table().' SET data = :data, unixtime = :time WHERE id = :id');
$stmp->execute([':id' => $session_id, ':data' => $session_data, ':time' => time()]);
}
public function destroy($session_id){
$stmp = $this->db->prepare('DELETE FROM '.$this->get_table().' WHERE id = :id');
$stmp->exe
db = $pdo;
$this->table_name = (is_null($table_name)) ? $this->table_name : $table_name;
$this->session_name = (is_null($session_name)) ? $this->session_name : $session_name;
$this->session_litetime = (is_null($session_lifetime)) ? $this->session_litetime : $session_lifetime;
session_set_save_handler(
[$this, 'open'], [$this, 'close'], [$this, 'read'], [$this, 'write'], [$this, 'destroy'], [$this, 'gc']
);
register_shutdown_function('session_write_close');
ini_set('session_auto_start', 0);
ini_set('session.cookie_httponly',1);
ini_set('session.use_only_cookies',1);
ini_set('session.gc_probability', 25);
ini_set('session.gc_divisor', 100);
ini_set('session.gc_maxlifetime', $this->session_litetime);
session_name($this->session_name);
session_start();
$this->regenerate_id();
}
private function __clone(){}
public function open($save_path, $session_name){
return true;
}
public function close(){
$this->gc($this->session_litetime);
return true;
}
public function read($session_id){
$session = $this->fetch_session($session_id);
return ($session === false) ? false : $session['data'];
}
public function write($session_id, $session_data){
$session = $this->fetch_session($session_id);
if($session === false)
$stmp = $this->db->prepare('INSERT INTO '.$this->get_table().' (id, data, unixtime) VALUES (:id, :data, :time)');
else
$stmp = $this->db->prepare('UPDATE '.$this->get_table().' SET data = :data, unixtime = :time WHERE id = :id');
$stmp->execute([':id' => $session_id, ':data' => $session_data, ':time' => time()]);
}
public function destroy($session_id){
$stmp = $this->db->prepare('DELETE FROM '.$this->get_table().' WHERE id = :id');
$stmp->exe
Solution
For one thing, to increase readability and cleanliness, try organizing your code a little better. You can read more on that here.
In
In
Hopefully you'll be adding to
Other than though, your connection looks good and you seem to be handling everything okay!
In
init(), you can use if(is_null(self::$instance)) instead of if(self::$instance == null).In
__construct(), I don't believe your ternaries need parentheses around the is_null functions.Hopefully you'll be adding to
open() because right now it has no purpose. And then in your setter and getter, I suppose you could switch the if statement around so instead of if (!self::check_dot($key)) you'll have if (self::check_dot($key)).Other than though, your connection looks good and you seem to be handling everything okay!
Context
StackExchange Code Review Q#39010, answer score: 3
Revisions (0)
No revisions yet.