The overview is to setup Django to use a file-based session system and
point it to the same files that PHP is using. Then override some
methods to help Python read and write the PHP arrays stored in the
session file. Now, if you can't use file-based sessions, this won't
specifically work for you, but may help in seeing what's possible.
First things first, this will make everything much easier:
http://hurring.com/scott/code/python/serialize/
In my case, I use files for maintaining the session data for my PHP
app. So, you'll need to setup your Django settings to use files for
session data and point it to the same folder (ie. /tmp/).
More info: http://www.djangoproject.com/documentation/sessions/#using-file-based-sessions
Then, create a file, I called mine handshake.py, to which you will
point your SESSION_ENGINE setting (note, this is different than what
the docs say above, because you are modifying the engine a bit to suit
your needs). In that file, you want to subclass SessionStore and
override the encode and decode methods. In each, you will use the
PHPSerialize or PHPUnserialize to work with the data and return it in
a form which Django can use.
Something like this:
from django.contrib.sessions.backends.file import SessionStore as
SessionFileStore
from PHPSerialize import *
from PHPUnserialize import *
class SessionStore(SessionFileStore):
'''
Howdy pardner! Speaking of pardners, I subclass the SessionStore
class in the file session backend.
This allows me intercept the decode and encode methods to grab the
PHP sessions and use them.
So, in a way, this makes me PHP's pardner, also.
'''
def __init__(self, session_key=None):
# call the super class's init
super(SessionStore, self).__init__(session_key)
#override the file_prefix
self.file_prefix = 'prefix_'
def decode(self, session_data):
# uses special session decode method in PHPUnserialize
u = PHPUnserialize()
return u.session_decode(session_data)
def encode(self, session_dict):
# users special encode method in PHPSerialize
s = PHPSerialize()
return s.session_encode(session_dict)
Remember to set your SESSION_COOKIE_NAME to the same name that your
PHP setup uses. This allows Django to find the session cookie on the
client machine.
You may not need to override the file_prefix as I have, but this will
depend on your setup.
Once this is all in place, you can just call request.session in your
views as you normally would. You can modify or add values and your PHP
app will see them just fine.
/alex
Our initial thoughts are to just call a Django login method from PHP
after the PHP login. Since they are sharing a session (essentially),
it may be that easy.
I'll let you know once we have something working.
/alex