01.04 php saved
bazil749
Tags add more
 
Note
Auth Component with Cookie
Works great thanX to TommyO!
  1. // app_controller:
  2. class AppController extends Controller {
  3.  
  4.     var $components = array('Auth', 'Cookie');
  5.    
  6.     function beforeFilter(){
  7.         $this->Auth->authorize = 'controller';
  8.         $this->Auth->loginAction = '/login/';
  9.         $this->Auth->autoRedirect = false;
  10.     }
  11.    
  12.     function isAuthorized() {
  13.         return true;
  14.    }
  15.  
  16. }
  17.  
  18. // users_controller:
  19.     function login()
  20.     {
  21.         if (empty($this->data)) {
  22.             $cookie = $this->Cookie->read('Auth.User');
  23.             if (!is_null($cookie)) {
  24.                 if ($this->Auth->login($cookie)) {
  25.                     //  Clear auth message, just in case we use it.
  26.                     $this->Session->del('Message.auth');
  27.                     $this->redirect($this->Auth->redirect());
  28.                 }
  29.             }
  30.         }
  31.         if ($this->Auth->user()) {
  32.             if (!empty($this->data)) {
  33.                 $cookie = array();
  34.                 $cookie['username'] = $this->data['User']['username'];
  35.                 $cookie['password'] = $this->data['User']['password'];
  36.                 $this->Cookie->write('Auth.User', $cookie, true, '+1 day');
  37.                 unset($this->data['User']['remember_me']);
  38.             }
  39.             $this->redirect($this->Auth->redirect());
  40.         }
  41.     }
  42.  
  43.     function logout(){
  44.         $this->Session->setFlash('Good-Bye');
  45.         $this->Cookie->del('Auth.User');
  46.         $this->redirect($this->Auth->logout());
  47.     }
  48.        
  49.     function beforeFilter() {
  50.         parent::beforeFilter();
  51.         $this->Auth->allow('add');
  52.     }
Parsed in 0.075 seconds, using GeSHi 1.0.7.14

Modify this Paste