3

It is not cross-domain. my site consists of php and django, I write a session in php , but i wanto read it in django, how can i do that??

StevenWang
  • 3,625
  • 4
  • 30
  • 40

1 Answers1

6

First, understand how PHP sessions work.

To access PHP's session data from Django, you need the session id & you need access to wherever the session data is stored.

To get the session id, retrieve it from the cookie; the cookie name should be set to the value of PHP's session.name setting. In Django, you can then get the session id from the cookie using request.COOKIES.get('<value of PHP's session.name setting>'). Obviously if you don't rely on cookies for maintaining session data then you'll need to take a different approach for this component.

To access the data, it gets a bit more complicated: you need to know where the session data is stored (PHP defaults to storing it in files) and a way of converting it to meaningful python objects.

To find out where the data is stored, use PHP's session_save_path() function to get the value of the session.save_path config setting. If you evaluate this properly, you'll be able to get the directory where the session files are stored. However, if the session data files are not obviously based on the session_ids (I don't have a PHP install handy to check) then you'll probably want to write your own SessionHandler and register it with set_session_save_handler() so that you know which files correspond to which session ids.

Once you know which session data corresponds to each session id, you can use python standard library functions to load & save data to & from the files. Unfortunately, I can't find a neat way to parse PHP's standard session serialization format (as defined by session.serialize_handler) as it doesn't match any standard format that I know of, but the format doesn't look too complex.

For example, based on this discussion, if pr($_SESSION); gives

Array 
( 
    [Config] => Array 
        ( 
            [rand] => 1482441247 
            [time] => 1179248446 
            [userAgent] => cc98eaffc23c634e0efd75ab9e36e810 
        ) 

)

then the session contents are

Config|a:3:{s:4:"rand";i:1482441247;s:4:"time";i:1179247983;s: 
9:"userAgent";s:32:"cc98eaffc23c634e0efd75ab9e36e810";}

You can see that it is saying that there is a variable called Config which is of type array length 3, and the array contents are made up of:

  • String of length 4 with value rand which is a key for an integer of value 1482441247
  • String of length 4 with value time which is a key for an integer of value 1179247983
  • String of length 9 with value userAgent which is a key for a string of length 32 with value cc98eaffc23c634e0efd75ab9e36e810

so with a little experimentation you should be able to write a little python module to take cookie data and convert it to a python list containing dicts & other standard python data objects.

Disclaimer: this is all based on research; I haven't tried this myself so edits & comments are welcome and, as always, reasons for any downvotes are greatly appreciated.

Community
  • 1
  • 1
Caspar
  • 7,039
  • 4
  • 29
  • 41