php sessionwriteclose,PHP Save Session when using session_write_close();

All of the answers here seem to be saying to use the session methods in ways that they were clearly not intended to be used...namely calling session_start() more than once.

The PHP website offers an example SessionHandlerInterface implementation that will work just like existing sessions but without locking the file. Just implementing their example interface fixed my locking issue to allow for concurrent connections on the same session without limiting my ability to add vars to the session. To prevent some race conditions, since the app's session isn't fully stateless, I did have to make a way to save the session mid-request without closing it so that important changes could save immediately after change and less important session vars could just save at the end of the request. See the below example for usage: Session::start(); echo("Vars Stored in Session Were:\n");print_r($_SESSION);echo(""); $_SESSION['one'] = 'one'; $_SESSION['two'] = 'two'; //save won't close session and subsequent request will show 'three' Session::save(); $_SESSION['three'] = 'three';

If you replace that Session::start() with session_start() and Session::save() with session_write_close(), you'll notice that subsequent requests will never print out the third variable...it will be lost. However, using the SessionHandler (below), no data is lost.

The OOP implementation requires PHP 5.4+. However, you can provide individual callback methods in older versions of PHP. See docs. namespace { class Session implements SessionHandlerInterface { /** @var Session */ private static $_instance; private $savePath; public static function start() { if( empty(self::$_instance) ) { self::$_instance = new self(); session_set_save_handler(self::$_instance,true); session_start(); } } public static function save() { if( empty(self::$_instance) ) { throw new \Exception("You cannot save a session before starting the session"); } self::$_instance->write(session_id(),session_encode()); } public function open($savePath, $sessionName) { $this->savePath = $savePath; if (!is_dir($this->savePath)) { mkdir($this->savePath, 0777); } return true; } public function close() { return true; } public function read($id) { return (string)@file_get_contents("$this->savePath/sess_$id"); } public function write($id, $data) { return file_put_contents("$this->savePath/sess_$id", $data) === false ? false : true; } public function destroy($id) { $file = "$this->savePath/sess_$id"; if (file_exists($file)) { unlink($file); } return true; } public function gc($maxlifetime) { foreach (glob("$this->savePath/sess_*") as $file) { if (filemtime($file) + $maxlifetime

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值