Recent changes

2013-04-14 2013-04-02 2013-02-17 2013-01-29 2013-01-20 2013-01-18 2012-12-08 2012-11-06 2012-09-04 2012-08-02

php/Mojavi/CookieFilter

SessionをCookieで持つ?なんか危険だな。 ちょっと自分が思ってたのと違うFilterだった。

ネタ元 <cat> <?php

  

class CookieFilter extends Filter {

  function execute (&$filterChain, &$controller, &$request, &$user)
  {
     // see if cookies are already loaded
     $loaded =& $request->getAttribute('CookiesLoaded');
     if ($loaded == NULL)
     {
        // set the cookies loaded request attribute so we don't reload them
        $request->setAttribute('CookiesLoaded', true);
        
        // load user values from the cookie (if available)
        if (isset($_COOKIE['TheLostRevue']))
        {
           $user_data = unserialize($_COOKIE['TheLostRevue']);
           foreach ($user_data as $namespace=>$data)
           {
               foreach ($data as $name=>$value)
               {
                   $user->setAttribute($name, $value, $namespace);
               }
           }
        }
        
        // execute chain
        $filterChain->execute($controller, $request, $user);
       // get user attribute namespaces & set in cookie
       $namespaces = $user->getAttributeNamespaces();
       $user_data = array();
       foreach ($namespaces as $namespace)
       {
           $user_data[$namespace] = $user->getAttributes($namespace);
       }
       
       setcookie('TheLostRevue', serialize($user_data),time()+60*60*24*30);
        // remove renderer
        $request->removeAttribute('CookiesLoaded');
     } else
     {
        $filterChain->execute($controller, $request, $user);
     }
  }

}

?> </cat>