Zend Framework实现将session存储在memcache中的方法_PHP
本文实例讲述了Zend Framework实现将session存储在memcache中的方法。分享给大家供大家参考,具体如下:
在zend framework中,已经可以将session存储在数据库中了,不过还不支持memcache,我简单得实现了一下。
下面是SaveHandler,文件名为 :Memcached.php ,将其放在 /Zend/Session/SaveHandler 目录下,代码如下(需要有php_memcache支持,因为字符长度限制,我把部分注释去掉了):
require_once 'Zend/Session.php'; require_once 'Zend/Config.php'; class Zend_Session_SaveHandler_Memcached implements Zend_Session_SaveHandler_Interface { const LIFETIME = 'lifetime'; const OVERRIDE_LIFETIME = 'overrideLifetime'; const MEMCACHED = 'memcached'; protected $_lifetime = false; protected $_overrideLifetime = false; protected $_sessionSavePath; protected $_sessionName; protected $_memcached; /** * Constructor * * $config is an instance of Zend_Config or an array of key/value pairs containing configuration options for * Zend_Session_SaveHandler_Memcached . These are the configuration options for * Zend_Session_SaveHandler_Memcached: * * * sessionId => The id of the current session * sessionName => The name of the current session * sessionSavePath => The save path of the current session * * modified => (string) Session last modification time column * * lifetime => (integer) Session lifetime (optional; default: ini_get('session.gc_maxlifetime')) * * overrideLifetime => (boolean) Whether or not the lifetime of an existing session should be overridden * (optional; default: false) * * @param Zend_Config|array $config User-provided configuration * @return void * @throws Zend_Session_SaveHandler_Exception */ public function __construct($config) { if ($config instanceof Zend_Config) { $config = $config->toArray(); } else if (!is_array($config)) { /** * @see Zend_Session_SaveHandler_Exception */ require_once 'Zend/Session/SaveHandler/Exception.php'; throw new Zend_Session_SaveHandler_Exception( '$config must be an instance of Zend_Config or array of key/value pairs containing ' . 'configuration options for Zend_Session_SaveHandler_Memcached .'); } foreach ($config as $key => $value) { do { switch ($key) { case self::MEMCACHED: $this->createMemcached($value); break; case self::LIFETIME: $this->setLifetime($value); break; case self::OVERRIDE_LIFETIME: $this->setOverrideLifetime($value); break; default: // unrecognized options passed to parent::__construct() break 2; } unset($config[$key]); } while (false); } } /** * 创建memcached连接对象 * * @return void */ public function createMemcached($config){ $mc = new Memcache; foreach ($config as $value){ $mc->addServer($value['ip'], $value['port']); } $this->_memcached = $mc; } public function __destruct() { Zend_Session::writeClose(); } /** * Set session lifetime and optional whether or not the lifetime of an existing session should be overridden * * $lifetime === false resets lifetime to session.gc_maxlifetime * * @param int $lifetime * @param boolean $overrideLifetime (optional) * @return Zend_Session_SaveHandler_Memcached */ public function setLifetime($lifetime, $overrideLifetime = null) { if ($lifetime < 0) { /** * @see Zend_Session_SaveHandler_Exception */ require_once 'Zend/Session/SaveHandler/Exception.php'; throw new Zend_Session_SaveHandler_Exception(); } else if (empty($lifetime)) { $this->_lifetime = (int) ini_get('session.gc_maxlifetime'); } else { $this->_lifetime = (int) $lifetime; } if ($overrideLifetime != null) { $this->setOverrideLifetime($overrideLifetime); } return $this; } /** * Retrieve session lifetime * * @return int */ public function getLifetime() { return $this->_lifetime; } /** * Set whether or not the lifetime of an existing session should be overridden * * @param boolean $overrideLifetime * @return Zend_Session_SaveHandler_Memcached */ public function setOverrideLifetime($overrideLifetime) { $this->_overrideLifetime = (boolean) $overrideLifetime; return $this; } public function getOverrideLifetime() { return $this->_overrideLifetime; } /** * Retrieve session lifetime considering * * @param array $value * @return int */ public function open($save_path, $name) { $this->_sessionSavePath = $save_path; $this->_sessionName = $name; return true; } /** * Retrieve session expiration time * * @param * @param array $value * @return int */ public function close() { return true; } public function read($id) { $return = ''; $value = $this->_memcached->get($id); //获取数据 if ($value) { if ($this->_getExpirationTime($value) > time()) { $return = $value['data']; } else { $this->destroy($id); } } return $return; } public function write($id, $data) { $return = false; $insertDate = array('modified' => time(), 'data' => (string) $data); $value = $this->_memcached->get($id); //获取数据 if ($value) { $insertDate['lifetime'] = $this->_getLifetime($value); if ($this->_memcached->replace($id,$insertDate)) { $return = true; } } else { $insertDate['lifetime'] = $this->_lifetime; if ($this->_memcached->add($id, $insertDate,false,$this->_lifetime)) { $return = true; } } return $return; } public function destroy($id) { $return = false; if ($this->_memcached->delete($id)) { $return = true; } return $return; } public function gc($maxlifetime) { return true; } protected function _getLifetime($value) { $return = $this->_lifetime; if (!$this->_overrideLifetime) { $return = (int) $value['lifetime']; } return $return; } protected function _getExpirationTime($value) { return (int) $value['modified'] + $this->_getLifetime($value); } }
配置(可以添加多台memcache服务器,做分布式):
$config = array( 'memcached'=> array( array( 'ip'=>'192.168.0.1', 'port'=>11211 ) ), 'lifetime' =>123334 ); //create your Zend_Session_SaveHandler_DbTable and //set the save handler for Zend_Session Zend_Session::setSaveHandler(new Zend_Session_SaveHandler_Memcached($config)); //start your session! Zend_Session::start();
配置好后,session的使用方法和以前一样,不用管底层是怎么实现的!
更多关于zend相关内容感兴趣的读者可查看本站专题:《Zend FrameWork框架入门教程》、《php优秀开发框架总结》、《Yii框架入门及常用技巧总结》、《ThinkPHP入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家基于Zend Framework框架的PHP程序设计有所帮助。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック









開発者とエンド ユーザーが Windows 上で最新バージョンのアプリケーションを実行するには、.NET Framework 4 が必要です。しかし、.NET Framework 4 のダウンロードとインストール中に、多くのユーザーから、インストーラーが途中で停止し、「ダウンロードがエラー コード 0x800c0006 で失敗したため、.NET Framework 4 はインストールされていません」というエラー メッセージが表示されるという苦情が寄せられました。デバイスに .NETFramework4 をインストールしているときにもこの問題が発生する場合は、正しい場所にいます。

Windows 11 または Windows 10 PC でアップグレードまたはアップデートの問題が発生すると、通常、失敗の背後にある実際の理由を示すエラー コードが表示されます。ただし、エラー コードが表示されずにアップグレードまたはアップデートが失敗すると、混乱が生じることがあります。便利なエラー コードを使用すると、問題の場所が正確にわかるので、修正を試みることができます。ただし、エラー コードが表示されないため、問題を特定して解決することが困難になります。エラーの背後にある理由を見つけるだけでも多くの時間がかかります。この場合、Microsoft が提供する SetupDiag という専用ツールを使用すると、エラーの背後にある本当の理由を簡単に特定できます。

Springboot プロジェクトの本番環境のセッションアウト タイムアウトで問題が見つかりました。問題の説明は次のとおりです: テスト環境では、application.yaml を変更することでセッションアウトが構成されていました。別の時間を設定してセッションアウト構成を確認した後、有効期限がリリース時に直接 8 時間に設定され、運用環境に到着しました。しかし、正午にお客様から、プロジェクトの有効期限が短く設定されており、30分操作がないとセッションが期限切れになり、再度ログインが必要になるというフィードバックをいただきました。開発環境の扱いの問題を解決します。springboot プロジェクトには Tomcat が組み込まれているため、プロジェクト内の application.yaml で設定されたセッションアウトが有効になります。本番環境: 本番環境リリースは

セッション障害は通常、セッションの有効期間の期限切れまたはサーバーのシャットダウンによって発生します。解決策: 1. セッションの有効期間を延長する; 2. 永続ストレージを使用する; 3. Cookie を使用する; 4. セッションを非同期的に更新する; 5. セッション管理ミドルウェアを使用する。

PHPSession のクロスドメイン問題の解決策 フロントエンドとバックエンドの分離の開発では、クロスドメイン要求が標準になっています。クロスドメインの問題に対処するときは、通常、セッションの使用と管理が必要になります。ただし、ブラウザーのオリジンポリシーの制限により、デフォルトではセッションをドメイン間で共有できません。この問題を解決するには、いくつかの技術と方法を使用して、セッションのクロスドメイン共有を実現する必要があります。 1. ドメイン間でセッションを共有するための Cookie の最も一般的な使用法
![SCNotification が動作を停止しました [問題を解決する 5 つのステップ]](https://img.php.cn/upload/article/000/887/227/168433050522031.png?x-oss-process=image/resize,m_fill,h_207,w_330)
Windows ユーザーは、コンピュータを起動するたびに「SCNotification は動作を停止しました」というエラーに遭遇する可能性があります。 SCNotification.exe は Microsoft システム通知ファイルであり、アクセス許可エラーやネットワーク障害が原因で PC を起動するたびにクラッシュします。このエラーは、問題のあるイベント名でも知られています。したがって、これは SCNotification が動作を停止したということではなく、バグ clr20r3 として認識される可能性があります。この記事では、SCNotification が動作を停止した問題を修正して再び悩まされないようにするために必要なすべての手順を説明します。 SCNotification.eとは何ですか

更新後に PHP セッションが消える問題の解決策: 1. 「session_start();」を通じてセッションを開きます; 2. すべてのパブリック設定を PHP ファイルに書き込みます; 3. 変数名は配列の添字と同じにすることはできません。 4. phpinfoでセッションデータの保存パスを確認し、ファイルディレクトリ内のsessioが正常に保存されているか確認してください。

セッション PHP のデフォルトの有効期限は 1440 秒、つまり 24 分です。つまり、クライアントが 24 分を超えて更新されない場合、現在のセッションは期限切れになります。ユーザーがブラウザを閉じると、セッションは終了し、セッションは存在しなくなります。
