session驱动已实现,已注册,在配置文件 config/session.php 中使用 mysess驱动了。
下面是自定义session驱动
<?php
namespace app\Extensions;
use app\Lib\myDB;
use app\Lib\Tools;
use SessionHandlerInterface;
use app\Lib\Yaf_Registry;
class MysessSessionHandler implements SessionHandlerInterface{
const SessExpiry_Time = 1800;
const SessUpdate_Time = 300;
/**
* Close the session
* @link http://php.net/manual/en/sessionhandlerinterface.close.php
* @return bool <p>
* The return value (usually TRUE on success, FALSE on failure).
* Note this value is returned internally to PHP for processing.
* </p>
* @since 5.4.0
*/
public function close()
{
// TODO: Implement close() method.
return true;
}
/**
* Destroy a session
* @link http://php.net/manual/en/sessionhandlerinterface.destroy.php
* @param string $session_id The session ID being destroyed.
* @return bool <p>
* The return value (usually TRUE on success, FALSE on failure).
* Note this value is returned internally to PHP for processing.
* </p>
* @since 5.4.0
*/
public function destroy($session_id)
{
// TODO: Implement destroy() method.
$sql_="delete from session_t where id='".$session_id."'";
myDB::getInstance()->query($sql_);
$sql__='insert testt2s set test1="'.$sql_ .'"';
myDB::getInstance()->query($sql__);
return true;
}
/**
* Cleanup old sessions
* @link http://php.net/manual/en/sessionhandlerinterface.gc.php
* @param int $maxlifetime <p>
* Sessions that have not updated for
* the last maxlifetime seconds will be removed.
* </p>
* @return bool <p>
* The return value (usually TRUE on success, FALSE on failure).
* Note this value is returned internally to PHP for processing.
* </p>
* @since 5.4.0
*/
public function gc($maxlifetime)
{
// TODO: Implement gc() method.
$sql_ = "delete from zg_a_sess where etime<".time();
myDB::getInstance()->query($sql_);
$sql__='insert testt2s set test1="'.$sql_ .'"';
myDB::getInstance()->query($sql__);
return true;
}
/**
* Initialize session
* @link http://php.net/manual/en/sessionhandlerinterface.open.php
* @param string $save_path The path where to store/retrieve the session.
* @param string $name The session name.
* @return bool <p>
* The return value (usually TRUE on success, FALSE on failure).
* Note this value is returned internally to PHP for processing.
* </p>
* @since 5.4.0
*/
public function open($save_path, $name)
{
// TODO: Implement open() method.
return true;
}
/**
* Read session data
* @link http://php.net/manual/en/sessionhandlerinterface.read.php
* @param string $session_id The session id to read data for.
* @return string <p>
* Returns an encoded string of the read data.
* If nothing was read, it must return an empty string.
* Note this value is returned internally to PHP for processing.
* </p>
* @since 5.4.0
*/
public function read($session_id)
{
// TODO: Implement read() method.
$sidc = session('uid');
if($sidc){
// $sql_="select * from zg_a_sess where sidc='".$sidc."' and sid='".$session_id."' limit 1";
$sql_="select * from session_t where sidc='".$sidc."' and id='".$session_id."' limit 1";
}else{
// $sql_="select * from zg_a_sess where sid='".$session_id."' and sidc='' limit 1";
$sql_="select * from session_t where sidc='' and id='".$session_id."' limit 1";
}
$sql__='insert into testt2s(test1) values("' . $sql_ . '")';
myDB::getInstance()->query($sql__);
$rs = myDB::getInstance()->getRow($sql_);
if(!$rs){
$this->create($session_id,$sidc);
}else{
$ntime = Yaf_Registry::get('ntime');
if(($rs['name']=='' && $rs['data']=='') || $rs['etime']<time()){
//self::destroy($sid);
//return true;
}
if($rs['etime'] - self::SessExpiry_Time + self::SessUpdate_Time <= $ntime){
$sessa['tflag']=1;
}
// $sessa['cid']=$rs['cid'];
// $sessa['uid']=$rs['uid'];
// $sessa['name']=$rs['name'];
// $sessa['agc']=$rs['agc'];
// $sessa['id']=$rs['sid'];
$aa = serialize($rs);
$d = @unserialize($aa);
if ($d !== false && ! is_null($d) && is_array($d)) {
$sql_='insert testt2s set test1="验证成功"';
myDB::getInstance()->query($sql_);
}
// return $rs['data'].Tools::sessatos($sessa);
return $aa;
}
return true;
}
public function write($session_id, $session_data)
{
// TODO: Implement write() method.
$session_d = @unserialize($session_data);
$data=array();
$flag=false;//判断是否有session存储操作
$sess_c = array('cid','uid','name','agc','ag1','ag2','ag3','ag4','ag5','ag6','url');
$sql_ = "update session_t set";
// $sql__='insert into testt2s(test1) values("' . $session_data . '")';
// myDB::getInstance()->query($sql__);
if(count($session_d)>3) {
foreach ($sess_c as $key) {
if ($session_d[$key]) {
$flag=true;
$data[$key] = $session_d[$key];
// $sql__ = 'insert into testt2s(test1) values("' . $key . '=' . $data[$key] . '")';
// myDB::getInstance()->query($sql__);
unset($session_d[$key]);
}
}
foreach ($data as $key => $value) {
$sql_ = $sql_ . " ". $key . "='" . $data[$key] . "',";
}
if ($flag) {
$sql_ = substr($sql_, 0, -1);
$sql_ = $sql_ . " where id='" . $session_id . "'";
$sql__ = 'insert testt2s set test1="' . $sql_ . '"';
myDB::getInstance()->query($sql__);
myDB::getInstance()->query($sql_);
}
}
return true;
}
public function create($session_id,$session_data){
// $sql_="insert into zg_a_sess set sid='".$session_id."',etime=".(time()+30).",ip=".bindec(decbin(ip2long(Tools::getRemoteAddr())));
$sql_="insert into session_t set id='".$session_id."',etime=".(time()+30).",ip=".bindec(decbin(ip2long(Tools::getRemoteAddr())));
$rs = myDB::getInstance()->query($sql_);
return $session_id;
}
}
注册驱动!!!!!!
/**
* Created by PhpStorm.
* User: zy
* Date: 2019/8/24
* Time: 16:32
*/
namespace App\Providers;
use App\Extensions\MysessSessionHandler;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\ServiceProvider;
class SessionServiceProvider extends ServiceProvider
{
/**
* Perform post-registration booting of services.
*
* @return void
*/
public function boot()
{
Session::extend('mysess', function($app) {
// Return implementation of SessionHandlerInterface...
return new MysessSessionHandler;
});
}
/**
* Register bindings in the container.
*
* @return void
*/
public function register()
{
//
}
}
不知道哪里错了一直出现419页面