开发者

Best way to get session id in Kohana 3?

What is the best way to get the current visitors session id in Kohana v3? session_id() doesn't seem to work a开发者_Go百科nd 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


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 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

Session::instance()->id();

It works for me. hope it works for you as well!


Look this commit http://github.com/kohana/core/commit/f67b07a69c4e16027ac52fda7a0747c510eee164


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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜