views:

124

answers:

2

Hi,

Is there a way to catch the contents of the PHP session variable $_SESSION['user_id'] with a mod_wsgi Python script? I'm running a script in the background that will decide whether or not the user may proceed to view the document.

I would like to do something like this:

def allow_access(environ, host):
    allow_access = False

    if environ['SCRIPT_NAME'] == 'forbidden_dir':
        if session['user_id'] == '1':
            allow_access = True

    if allow_access:
        return True
    else:
        return False

Is it possible?

+3  A: 

If it's possible, it's not easy; apache stores session variables in files in a special format.

Your best option might be to write a php page that prints all session variables. (Hard-code it to only serve to localhost.) Open the url to that page from within your python script. Add a header to the url request with the session info. Then, once the php page is loaded in Python, parse the input.

BernzSed
I think you will definitely need to have Python call your PHP pages as the $_SESSION vars are really an artifact of PHP, rather than Apache.
Kevin Horn
Aha, that would probably work well. I can see the solution clearly :)
Christoffer
+2  A: 

please don't do this:

if allow_access:
    return True
else:
    return False

when you can do: return allow_access.

SilentGhost
Sorry, I have no idea what I was thinking. :/
Christoffer