首頁 > php教程 > PHP源码 > 主體

Session Manager by Redis

PHP中文网
發布: 2016-05-26 08:19:05
原創
857 人瀏覽過

SessionManager.php 

<?php 
	/**
	* SessionManager
	*/
	class SessionManager
	{
		private $redis;
		private $sessionSavePath;
		private $sessionName;
		private $sessionExpireTime = 30;

		function __construct()
		{
			$this->redis = new Predis\client();
			$this->redis->connect(&#39;127.0.0.1&#39;, 6379);

			$retval = session_set_save_handler(
				array($this, "open"),
				array($this, "close"),
				array($this, "read"),
				array($this, "write"),
				array($this, "destroy"),
				array($this, "gc")
				);

			session_start();
		}

		public function open($patn, $name){
			return true;
		}

		public function close()
		{
			return true;
		}

		public function read($id)
		{
			$value = $this->redis->get($id);
			if ($value) {
				return $value;
			}else{
				return &#39;&#39;;
			}
		}

		public function write($id, $data)
		{
			var_dump($id);
			if ($this->redis->set($id, $data)) {
				$this->redis->expire($id, $this->sessionExpireTime);
				return true;
			}
			return false;
		}

		public function destroy($id)
		{
			if ($this->redis->delete($id)) {
				return true;
			}
			return false;
		}

		public function gc($maxlifetime)
		{
			return true;
		}

		public function __destruct()
		{
			session_write_close();
		}
	}
 ?>
登入後複製

session_set.php

<?php 
	require &#39;../Predis/src/Autoloader.php&#39;;

	Predis\Autoloader::register();

	include("SessionManager.php");
	new SessionManager();

	$_SESSION[&#39;username&#39;] = "xugang";

	echo "<a href = &#39;./session_get.php&#39;>session</a>";
 ?>
登入後複製

session_get.php

<?php 
	require &#39;../Predis/src/Autoloader.php&#39;;

	Predis\Autoloader::register();

	include("SessionManager.php");
	new SessionManager();
	echo $_SESSION[&#39;username&#39;];
 ?>
登入後複製

                               

                   

相關標籤:
php
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!