Ruby on Rails makes nice with PHP

Not all projects can jump ship from a home grown PHP site to Rails, especially projects where there are exactly 1.3 programmers for the project. I was surprised there wasn't much information on integrating php and rails site (spare me the flame wars). All new development will be done in Rails and old PHP will be written in Rails as time permits. I was exploring some of the ways that this could be possible and what possible problems might arise.

I will need to read certain sessions variables from PHP into Rails, I will not need to write to the session. I found a ruby script PHP Serialize which takes a php serialized string and converts it to a Ruby datatype. If your sessions are stored as files, they are in /tmp/sess_[session id] where session id stored in $PHPSESSID they contain a special serialized string. Ruby can get the session id from the cookie and read the file, and PHP Serialize will convert it into an object. If your sessions are stored in a database, then of course you'll be able to create a Rails model to access it. If you need to write to a php session, then I think it would be better this way. I didn't try using sessions stored in a database, because I use files. I’m not sure how you would do it stored in memory. In any case, reading from the session file I believe is a temporary hack at best.

File Structure
A rails site is probably different than your file structure for a PHP site. The traditional site has a htdocs and if you are smart you keep most of your code, database configuration, etc outside the doc root and they are only accessed by files in the htdocs. Rails really isn't so different in structure: Instead of htdocs, it has public (gee, makes sense) and most ruby code is in the app directory. Other ideas I’ve heard are using sym links, but I can’t really see that working out very well, or really what benefit would there be?.

That’s what I’ve found so far, I will post any gotchas or problems we encounter in this brand new world.

Anybody else have an experience with integrating?


  • PHPSession, - ruby code for reading serialized php sessions and strings

It would

if we made heavy use of sessions. We only have one variable that we will need to check in ruby, so its not worth the effort right now. In fact, in PHP we hardly used sessions variables (most of it is early php 4 for that project) ... but its nice to have that as a PLAN B just in case. :)