CakePHP Session without Cookies
I开发者_开发问答 want to make an application that doesn't need cookies but still has Sessions. Therefor I create the SessionID and take it in the URL with me. My problem now is that the HTML helper doesn't recognize this and all links that are outputted are without the session id.
Does CakePHP have to be told to do that or isn't there any mechanism for this? (taking the session id in every link)
I just made a url session implementation in cakephp, I set cakephp to use trans_sid and not cookies:
ini_set("session.use_cookies",0);
ini_set("session.use_trans_sid",1);
this could probably be done using: http://bakery.cakephp.org/articles/admad/2009/09/02/how-to-bend-cakephp-s-session-handling-to-your-needs and used a beforefilter to read a session id form url parameter and set session id from that, and cookie if session id was not set as url parameter
// Use sid from post
if (!empty($this->params['url']['sid'])) {
$this->Session->id($this->params['url']['sid']);
}
// Or failover to cookie
else
{
$this->Session->id($this->Cookie->read('CAKEPHP'));
}
Remember to override the url function in the AppHelper class
function url($url = null, $full = false) {
return parent::url($url, $full)."/?sid=".session_id();
}
and redirect and flash in the AppController class
function redirect($url, $status = null, $exit = true) {
$url = $url."?sid=".session_id();
return parent::redirect($url, $status, $exit);
}
function flash($message, $url, $pause = 1, $layout = 'flash') {
$url = $url."?sid=".session_id();
return parent::flash($message, $url, $pause, $layout);
}
This is a case of "there's still PHP in CakePHP." The default HtmlHelper does not attach any session values as far as I'm aware, since Cake favors cookie sessions. You can use the standard PHP tools to change that though.
First, you'll want to customize the cookie handling. See here for details: How to bend CakePHP's session handling to your needs. Then, you'll need to decide whether to use PHP's trans-sid
functionality, or use the SID
constant as a means to transfer the session id. If the latter, you may want to create a custom AppHelper::url
method to automatically include it in every link. See here for a solution to a similar problem: Adding a prefix to every URL in CakePHP.
精彩评论