Thursday, January 21, 2010

Inspecting PHP sessions from Python

For one of our PHP projects we wanted to be able to inspect the PHP sessions from outside PHP. For example we want to know the users privileges at certain moment, i.e. if the user is logged in or not.

Why would you need that you may ask?

Well, let's say that our symfony application stores the result of a cached action in Memcache, having two versions of the result HTML, one for logged in users and one for logged out ones. In that case we want to avoid loading symfony at all and returning directly the HTML from Nginx. One of our devs wrote a Nginx module that does just that, it gets from the Memcache certain value, if it's found, then it returns the HTML immediately, else it calls symfony to handle the request. The problem with this approach is that the Nginx doesn't know if the user is authenticated or not, so it can't handle the case where we have two different versions of HTML output for one action. Well, until now...

Please welcome InspectorD a Python daemon that can inspect PHP sessions.

InspectorD is tcp server that understands a very simple text protocol: you ask it if certain session_id is authenticated and it replies 1 if it does, or 0 if it doesn't.

Here's an sample session:

telnet localhost 3002
isauth oglnp9phvn8ac04obdqjk6dko3
0
isauth bj6sc485t9s46o57qpngod5lm7
1
isauth bj6sc485t9s46o57qpngod5lm7 oglnp9phvn8ac04obdqjk6dko3 n63o4uk297c49131dcdg0h7g72
1
0
1
quit

The server is based on the Twisted framework and the PHPUnserialize module by Scott Hurring. From the later I fixed the session_decode method since it wasn't working for me.

For installation instructions and usage see the github project page.

Any comments and bugs reports are welcomed.

Thursday, January 14, 2010

Running JLang on Snow Leopard

For this new year I started playing with J, a language that I discovered last year –but that I've never found time to play with–, while installing it, I had the problem that I didn't run on Snow Leopard.

After asking on twitter on how to solve this problem, @kaleidic pointed me to this guide, which tells you to modify the Java preferences to run in 32 bit mode. What I didn't liked from that approach is that it seems to modify the general preferences of Java. After some searches I found that there's an option for the java executable -d32, that makes it run in 32 bits.

To apply this option simply go to the place where you installed J and there edit bin/jwd.

In that file you will find a line like this:

java -Xss8000000 -Xdock:name=J -Xdock:icon=bin/icons/jred.icns -jar bin/j.jar "$@"

which you have to modify to:

java -d32 -Xss8000000 -Xdock:name=J -Xdock:icon=bin/icons/jred.icns -jar bin/j.jar "$@"

And that's it, then simply double click on the J icon and it should work as expected.

NOTE: The line breaks that you see in the execution code are inserted by blogger, as you'll see on the jwd file, there are no line breaks there.