note 118225 added to function.session-set-save-handler
| From: | andreipa at gmail dot com | Date: | Thu, 29 Oct 2015 15:05:11 +0000 |
| Subject: | note 118225 added to function.session-set-save-handler | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-204049@lists.php.net to get a copy of this message | ||
After spend so many time to understand how PHP session works with database and unsuccessful attempts
to get it right, I decided to rewrite the version from our friend stalker.
//Database
CREATE TABLE
Session (
Session_Id varchar(255) COLLATE utf8_unicode_ci NOT NULL,
Session_Expires datetime NOT NULL,
Session_Data text COLLATE utf8_unicode_ci,
PRIMARY KEY (Session_Id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
SELECT * FROM mydatabase.Session;
<?php
//inc.session.php
class SysSession implements SessionHandlerInterface
{
private $link;
public function open($savePath, $sessionName)
{
$link =
mysqli_connect("server","user","pwd","mydatabase");
if($link){
$this->link = $link;
return true;
}else{
return false;
}
}
public function close()
{
mysqli_close($this->link);
return true;
}
public function read($id)
{
$result = mysqli_query($this->link,"SELECT Session_Data FROM Session WHERE
Session_Id = '".$id."' AND Session_Expires > '".date('Y-m-d
H:i:s')."'");
if($row = mysqli_fetch_assoc($result)){
return $row['Session_Data'];
}else{
return "";
}
}
public function write($id, $data)
{
$DateTime = date('Y-m-d H:i:s');
$NewDateTime = date('Y-m-d H:i:s',strtotime($DateTime.' + 1 hour'));
$result = mysqli_query($this->link,"REPLACE INTO Session SET Session_Id =
'".$id."', Session_Expires = '".$NewDateTime."',
Session_Data = '".$data."'");
if($result){
return true;
}else{
return false;
}
}
public function destroy($id)
{
$result = mysqli_query($this->link,"DELETE FROM Session WHERE Session_Id
='".$id."'");
if($result){
return true;
}else{
return false;
}
}
public function gc($maxlifetime)
{
$result = mysqli_query($this->link,"DELETE FROM Session WHERE
((UNIX_TIMESTAMP(Session_Expires) + ".$maxlifetime.") <
".$maxlifetime.")");
if($result){
return true;
}else{
return false;
}
}
}
$handler = new SysSession();
session_set_save_handler($handler, true);
?>
<?php
//page 1
require_once('inc.session.php');
session_start();
$_SESSION['var1'] = "My Portuguese text: SOU Gaucho!";
?>
<?php
//page 2
require_once('inc.session.php');
session_start();
if(isset($_SESSION['var1']){
echo $_SESSION['var1'];
}
//OUTPUT: My Portuguese text: SOU Gaucho!
?>
----
Server IP: 72.52.91.14
Probable Submitter: 188.39.76.19
----
Manual Page -- http://php.net/manual/en/function.session-set-save-handler.php
Edit -- https://master.php.net/note/edit/118225
Del: integrated -- https://master.php.net/note/delete/118225/integrated
Del: useless -- https://master.php.net/note/delete/118225/useless
Del: bad code -- https://master.php.net/note/delete/118225/bad+code
Del: spam -- https://master.php.net/note/delete/118225/spam
Del: non-english -- https://master.php.net/note/delete/118225/non-english
Del: in docs -- https://master.php.net/note/delete/118225/in+docs
Del: other reasons-- https://master.php.net/note/delete/118225
Reject -- https://master.php.net/note/reject/118225
Search -- https://master.php.net/manage/user-notes.php