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.
PHP网站提供了一个榜样,即“Handler Interface”执行,将像现有会议一样,但不会锁定档案。 仅仅落实它们的榜样,就把我lock锁的问题固定下来,以便在同届会议上同时建立联系,同时又不限制我增加届会次数的能力。 为了防止某些种族条件,由于本届会议没有完全的无国籍状态,我确实必须设法在未结束的情形下挽救会议,以便重大变革能够在变革之后立即拯救出来,而不太重要的几届会议只能在请求结束时挽救。 见以下使用实例:
Session::start();
echo("<pre>Vars Stored in Session Were:
");print_r($_SESSION);echo("</pre>");
$_SESSION[ one ] = one ;
$_SESSION[ two ] = two ;
//save won t close session and subsequent request will show three
Session::save();
$_SESSION[ three ] = three ;
如果您取代<代码>Session:start(> with session_start()
and :save(
with session_write_close()
,请您通知,以后的请求永远不会打印出第三个变数......否则将丢失。 然而,使用Handler会议(以下),没有数据。
执行《行动计划》需要PHP5.4+。 但是,你可以在旧版本的PHP上提供个人反馈方法:。 见。
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 < time() && file_exists($file)) {
unlink($file);
}
}
return true;
}
}