Awesome
Nginx PHP session
This is an Nginx module that can extract values out of a serialized PHP Session and store them into an Nginx variable to make them reusable in the Nginx configuration
Example
location / {
eval $session {
set $memcached_key "d41d8cd98f00b204e9800998ecf8427e:5b9hm9rspbgajg22eqpqs6ang1";
memcached_pass 192.168.1.237:11210;
}
php_session_parse $result $session "symfony/user/sfUser/attributes|s:10:\"subscriber\";s:9:\"getGender\"";
if ($result = "s:1:\"w\"")
{
# do something
}
if ($result = "s:1:\"m\"")
{
# do something else
}
default_type text/plain;
}
Array element specification syntax
This is a little hard to explain. First, the PHP session serialization is - for some mysterious reason - using two different characters for element separation, ; semicolon and | pipe. I will give an example:
if you store a multidimensional array in $_SESSION['symfony/user/sfUser/attributes'] the serialization of it might look like this:
symfony/user/sfUser/attributes|s:10:"subscriber";s:9:"getGender"
The type of the first element in a serialized PHP session is not defined, its always string. Thats why its type is also not defined in its serialized representation, while the type for other array keys is. The first level array element in the example has the key "symfony/user/sfUser/attributes". This element stores another, regular array which has a key of type string, key length 10 and value "subscriber". This is defined via 's:10:"subscriber"'. Then there is another sub array with a key of type string and length 9 and content "getGender", like this 's:9:"getGender"'. The best way to find the right notation to to specify a key in an array is to print it out as serialized and then read the serialized string that got generated by PHP. The above specified array path would find its way through a session array for example like this:
$_SESSION['symfony/user/sfUser/attributes'] => Array
(
[users_dynamic] => Array
(
[get_last_online_state] =>
[update_counter_time] => 1266041164
)
[subscriber] => Array
(
[user_actual_culture] => de
[lastURI] => http://dev.poppen.lab/frontend_dev.php/home
[invisibility] => 0
[getGender] => m
)
)
Stripping the formatting of the extracted values
After you extract a value from the session, using the above syntax, you get some string in a format like s:6:"DrEvil", but actually the value that you want is only DrEvil, you don't need PHP's definition of that this is a string and how long it is. For exactly that i recently added a function called php_session_strip_formatting.
You can use it like for example following:
location / {
set $my_formatted_string "s:6:\"DrEvil\"";
echo $my_formatted_string;
php_session_strip_formatting $my_stripped_string $my_formatted_string; # remove the formatting
echo $my_stripped_string;
return 200;
}
And the output will look like :
s:6:"DrEvil"
DrEvil
The php_session_strip_formatting supports the following serialized datatypes:
<b>Type</b> | <b>Identifier</b> |
---|---|
Boolean | b |
Integer | i |
Double | d |
String | s |
All other Datatypes will simply be ignored and the result variable will be left empty;
More Examples
<a href="http://mauro-stettler.blogspot.com/2010/02/variables-from-php-sessions-in-nginx.html">http://mauro-stettler.blogspot.com/2010/02/variables-from-php-sessions-in-nginx.html</a>
<a href="http://mauro-stettler.blogspot.com/2011/06/php-session-parser-in-production.html">http://mauro-stettler.blogspot.com/2011/06/php-session-parser-in-production.html</a>