您好,登錄后才能下訂單哦!
這篇文章給大家介紹如何在PHP中使用Redis共享Session,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
1. 通過php自身session配置實現
# 使用 redis 作為存儲方案 session.save_handler = redis session.save_path = "tcp://127.0.0.1:6379" # 若設置了連接密碼, 則使用如下 session.save_path = "tcp://127.0.0.1:6379?auth=密碼"
測試代碼
<?php ini_set("session.save_handler", "redis"); ini_set("session.save_path", "tcp://127.0.0.1:6379"); session_start(); echo "<pre>"; $_SESSION['usertest'.rand(1,5)]=1; var_dump($_SESSION); echo "</pre>";
輸出 ↓
array(2) {
["usertest1"]=>
int(88)
["usertest3"]=>
int(1)
}
usertest1|i:1;usertest3|i:1;
評價
優點: 實現簡單, 無需修改php代碼
缺點: 配置不支持多樣化, 只能應用于簡單場景
2. 設置用戶自定義會話存儲函數
通過 session_set_save_handler() 函數設置用戶自定義會話函數.
session_set_save_handler ( callable $open , callable $close , callable $read , callable $write , callable $destroy , callable $gc [, callable $create_sid [, callable $validate_sid [, callable $update_timestamp ]]] ) : bool # >= php5.4 session_set_save_handler ( object $sessionhandler [, bool $register_shutdown = TRUE ] ) : bool
在配置完會話存儲函數后, 再執行 session_start() 即可.
具體代碼略, 以下提供一份 Memcached 的(來自Symfony框架代碼):
<?php /* * This file is part of the Symfony package. * * (c) Fabien Potencier <fabien@symfony.com> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\HttpFoundation\Session\Storage\Handler; /** * MemcacheSessionHandler. * * @author Drak <drak@zikula.org> */ class MemcacheSessionHandler implements \SessionHandlerInterface { /** * @var \Memcache Memcache driver. */ private $memcache; /** * @var int Time to live in seconds */ private $ttl; /** * @var string Key prefix for shared environments. */ private $prefix; /** * Constructor. * * List of available options: * * prefix: The prefix to use for the memcache keys in order to avoid collision * * expiretime: The time to live in seconds * * @param \Memcache $memcache A \Memcache instance * @param array $options An associative array of Memcache options * * @throws \InvalidArgumentException When unsupported options are passed */ public function __construct(\Memcache $memcache, array $options = array()) { if ($diff = array_diff(array_keys($options), array('prefix', 'expiretime'))) { throw new \InvalidArgumentException(sprintf( 'The following options are not supported "%s"', implode(', ', $diff) )); } $this->memcache = $memcache; $this->ttl = isset($options['expiretime']) ? (int) $options['expiretime'] : 86400; $this->prefix = isset($options['prefix']) ? $options['prefix'] : 'sf2s'; } /** * {@inheritdoc} */ public function open($savePath, $sessionName) { return true; } /** * {@inheritdoc} */ public function close() { return $this->memcache->close(); } /** * {@inheritdoc} */ public function read($sessionId) { return $this->memcache->get($this->prefix.$sessionId) ?: ''; } /** * {@inheritdoc} */ public function write($sessionId, $data) { return $this->memcache->set($this->prefix.$sessionId, $data, 0, time() + $this->ttl); } /** * {@inheritdoc} */ public function destroy($sessionId) { return $this->memcache->delete($this->prefix.$sessionId); } /** * {@inheritdoc} */ public function gc($maxlifetime) { // not required here because memcache will auto expire the records anyhow. return true; } /** * Return a Memcache instance * * @return \Memcache */ protected function getMemcache() { return $this->memcache; } }
關于如何在PHP中使用Redis共享Session就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。