在 Kohana 3 中获取会话 ID 的最佳方法?
在 Kohana v3 中获取当前访客会话 ID 的最佳方法是什么? session_id()
似乎不起作用,只为我返回 null 。
目前我使用 cookie::get('session')
,但这并不'第一次访问该网站时无法工作,这是我需要做的。
我知道你可以在 Kohana v2 中执行 $this->session->id()
,但这在 KO3 中似乎不存在...
谢谢
What is the best way to get the current visitors session id in Kohana v3? session_id()
doesn't seem to work and only returns null for me..
At the moment Im using cookie::get('session')
, but that doesn't work on the first time you access the site, which I need to do.
I know you could do $this->session->id()
in Kohana v2, but that doesn't seem to exist in KO3...
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
通常,您使用以下 3 种会话类型中的任何一种:本机、Cookie、数据库。
对于cookie,我相信你没有会话id。使用 cookie 作为会话类型时,即使调用 Session::instance()->regenerate(),也只会返回 NULL。
对于本机,您应该能够在通过 Session::instance()->regenerate() 生成它时获取 ID,它返回会话 id。
对于数据库,Session_Database 已保护存储会话 ID 的 $_session_id。您可能想扩展该类并编写自己的 getter。
希望有帮助。
Typically you use any of these 3 session type: Native, Cookie, Database.
For cookie, I believe you don't have session id. Even if you call Session::instance()->regenerate() when using cookie as session type, it will only return NULL.
For native, you should be able to get the ID when generating it by Session::instance()->regenerate() which returns the session id.
For database, Session_Database has protected $_session_id where session id is stored. You may want to extend that class and write your own getter.
Hope that helps.
Kohana 有会话的包装类。该类称为会话。 Kohana 允许使用多个驱动程序(例如 memcached)以多种方式存储会话数据。我使用本机方式存储会话数据,如果您不通过 config/session.php 进行任何配置更改,则这是默认方式
我查看了本机会话的 kohana 代码,发现这将给出会话 id
它对我有用。希望它也适合你!
Kohana have wrapper class for sessions. That class is called Session. Kohana allows multiple ways to store session data using multiple drivers such as memcached. I am using native way to store session data, that is default if you don't make any config changes via config/session.php
I have looked kohana code for native session and found that this will give session id
It works for me. hope it works for you as well!
查看此提交
Look this commit http://github.com/kohana/core/commit/f67b07a69c4e16027ac52fda7a0747c510eee164
通过查看GitHub上的源代码,我似乎找不到返回id的方法。
但是,
session_id()
用于 本机驱动程序,所以也许它应该适合您的情况。也许您首先调用
session_regenerate_id()
?From looking at the source on GitHub, I can't seem to find a method for returning the id.
However,
session_id()
is used for the native driver, so perhaps it should work in your case.Perhaps if you call
session_regenerate_id()
first?如果您使用数据库驱动程序,则 cookie 将保存会话 ID。
另一方面,似乎没有直接的方法来获取会话 ID。
正如 @SyaZ 建议的那样,您应该扩展会话类并编写自己的 getter 方法。
If you're using the database driver then the cookie will hold the session id.
On the other hand it seems to be no direct way to get the session id.
As how @SyaZ suggested, you should extend the session class and write your own getter method.