Welcome to Working With Rails


Discussion Forums

Discuss all things Ruby on Rails with perhaps the web's most vibrant group of Ruby on Rails enthusiasts.
Moving seamlessly between Rails and PHP
4 Posts
Moving seamlessly between Rails and PHP

Ok, I've put up 100% Rails apps into production. But my new job is entirely written in PHP, and while the boss is open to switching to Rails, he's envisioning some proof of concept work where a user of the PHP site clicks a link, lands seamlessly on a Rails app, and then clicks links in the Rails app and is back in PHP world.

I'm pretty sure that I can get Apache to do the right thing by recognizing, for example, that the /faq/ directory should be served up by Mongrel. Where I lose it is in session control. What if my PHP user was logged in? Even if he wasn't, he had a sessionid, so can I pass that session id from PHP to Rails and then back again so I can make it as seamless as possible for the user?

I did something similar but a little simpler a while back. If I remember correctly PHP by default stores its sessions in files on the server. It's a configuratrion option. What you need to do is read off the PHP session cookie in the Rails app (default PHPSESSID), then find the corresponding session data in the filesystem, and then populate the Rails session with this data -> bam..you're good to go.

Remember PHPSESSID is just a cookie. If the Rails app is running on the same domain as the PHP app (which I assumed it is), you can just read it like any cookie on the Rails side. After that it'll be a bit of work, but you basically have all you need in front of you to make it work. There's no technical restrictions why you can't do it. Make it a before filter or something similar so it's as unobtrusive as possible. After that you can just concentrate on working the normal Rails way again, the before filter takes care of xferring the PHP session for you..


i did something similar, only with 2 Javascript libraries that had to work together for a while (JQuery and MooTools). i simply used an iFrame and set the outer content to one library, and inner to another one, i guess it should work for you too. The thing i don't understand is, how is it going to prove something for your boss?

I'd suggest doing what Casper said, except that if you need to go from Rails to PHP and back again then you should store a timestamp in the cookie session data as well (server side ofc). This will let you know which session was most recently touched and perform a reconciliation between the 2 session cookies.

You could also make the decision that the PHP session should be the 'master' and every time a change is made to the rails session, it is updated to the PHP one. This would be potentially simpler as it wouldn't require updating the PHP app at all.

4 Posts
Login to add your message