jlsalvador / yii-db-http-token-session
YiiDbHttpTokenSession 是一个 Yii 扩展,它通过使用数据库作为 token-session 连接器来扩展 CDbHttpSession,在每次请求-响应中无需使用 cookie 或 PHPSESSID(类似于 Rest Api 解决方案)。
dev-master
2013-10-06 14:26 UTC
This package is not auto-updated.
Last update: 2024-09-23 15:51:10 UTC
README
YiiDbHttpTokenSession 是一个 Yii 扩展,它通过使用数据库作为 token-session 连接器来扩展 CDbHttpSession,在每次请求-响应中无需使用 cookie 或 PHPSESSID(类似于 Rest Api 解决方案)。
工作原理
第一次您发起请求并且您的应用程序使用 PHP 会话时,会生成一个 token 并在 HTTP 响应头中输出,如 Token: 12345678901234567890123456789012。使用此 token 在下一次 HTTP 请求头中继续会话,如 Token: 12345678901234567890123456789012。您还可以使用 HTTP 参数 _t(默认情况下)。每次请求都会生成一个新的 Token,并将其发送到 HTTP 响应头。
要求
- PHP 5.3.0(未测试旧版本)
- YiiFramework 1.1.13(未测试旧版本)
安装
-
以下方式之一获取源代码
- 下载 最新版本并将文件放置在应用程序根目录下的
protected/extensions/YiiDbHttpTokenSession/目录下。 - 在应用程序根目录下将此存储库作为 git 子模块添加到您的存储库中:
git submodule add https://github.com/jlsalvador/YiiDbHttpTokenSession.git protected/extensions/YiiDbHttpTokenSession
- 下载 最新版本并将文件放置在应用程序根目录下的
-
编辑应用程序配置并将会话组件设置为 YiiDbHttpTokenSession
'components'=>array( 'session'=>array( 'class'=>'ext.YiiDbHttpTokenSession', 'connectionID'=>'db', // Set the database Yii component, it's optional. 'tokenRequestKeyName'=>'_t', // The $_REQUEST index name that will store a token id instead the HTTP header, defaults to '_t'. 'tokenHeaderKeyName'=>'HTTP_TOKEN', // The $_SERVER index name that will store a token id, defaults to 'HTTP_TOKEN'. 'tokenTimeout'=>1440, // The number of seconds after which data will be seen as garbage and cleaned up, defaults to 1440 seconds. 'tokenTableName'=>'YiiToken', // The token table name, defaults to 'YiiToken'. 'autoCreateTokenTable'=>true, // Whether the token DB table should be automatically created if not exists, defaults to true. ), ),
示例
JavaScript
var tokenId = '12345678901234567890123456789012'; // Set here your last token id from the HTTP header response. $.ajax({ beforeSend: function (xhr) { xhr.setRequestHeader('Token', tokenId); }, url:'http://my-site.com/api/work', type:'GET', success:function(data) { console.log(data); }, error:function (xhr, ajaxOptions, thrownError){ console.log(xhr.responseText); } }).then(function (data, textStatus, xhr) { tokenId = xhr.getResponseHeader('Token'); // This set the next token id for the next request. });
CURL
curl -i -H "Accept: application/json" -H "Token: 12345678901234567890123456789012" http://my-site.com/api/work
贡献者
许可
YiiDbHttpTokenSession 在 GNU Lesser General Public License 下发布。