Aug

14

During a recent project, I was asked to build an extension onto a site we had done many moons ago. The original site was just custom rolled PHP. No frameworks at all. However, with the new extension I decided to use CakePHP. The extension could live standalone, but needed to be “aware” of some data from the original app. More specifically, something similar to a login sets a session var upon success. I didn’t think it would be all that big of a deal to read that Session in Cake, after all it was still PHP on the same box, in the same application name space. I was wrong.

The explanation is pretty simple to understand. Cake has it’s own Session handling. Because of this Cake creates a Session automatically by default. This can be turned off in the configs, but really isn’t the root of the problem. The issue lies in Cake not being aware of the external app’s already created Session. There are many posts about injecting data from external Sessions into a Cake Session, but that doesn’t seem very DRY to me. It could cause issues with data being out of sync as well. A lot of management could need to occur.

So last night I decided to try something relatively simple in hind sight. I wanted to figure out a way for Cake to just use the external Session. Bottomline, it’s pretty easy. All you have to do is pass the session ID (session_id()) from the external to the Cake app. Querystring param or whatever. And then in your Cake controller, set a beforeFilter method that accepts the param and uses it to create a new Session, which is already created, so Cake just uses it.

if (isset($_GET['SESSIONID'])) { session_destroy(); session_id($_GET['SESSIONID']); session_start(); }

After that, you can access any properties from the external session in Cake natively using Cake’s built in methods like:
$this->Session->read('param_name');

or just through the PHP Session Object:

$_SESSION['param_name']

Aug

1

Anniversary

August 1, 2008 | Leave a Comment

I’ve been blogging for over 6 years now. I’m old!

Aug

1

Reason #488 to hate IE6. I just finished banging my head against the IE6 wall again, trying to figure out why a simple getURL call to a JavaScript function would happen randomly and fail randomly. Works fine 100% of the time in other browsers, including IE7. The call was simple: getURL(”javascript:swapBG(true)”); nothing fancy, no dynamic vars, nothing. It would work correctly the first two or three times, then all of the sudden The Flash Player and IE6 would treat the “javascript:swapBG(true)” as a URL string versus a JS call and would place the string in the address bar and try and refresh the page. Huh? No idea why it would work correctly the first few times and then stop. No popup action, so no blocking. All other JavaScript calls in the Flash were working. Just this one particular call. Odd. Tried nearly everything. Moved it to another frame. Added return false; to the AS and the JS. Nothing.

In the end, I switched it to an ExternalInterface call and magically it works consistently. Fun. Actually not so much.

Linkroll

Recent Projects