06.05 php saved
stevieboy
Tags add more
simple auth login  
Note
This is the users_controller.php
  1. <?php
  2. class UsersController extends AppController {
  3.  
  4.     var $name = 'Users';
  5.     var $helpers = array('Html', 'Form', 'Javascript' );
  6.  
  7.     function login()
  8.     {
  9.         //Don't show the error message if no data has been submitted.
  10.         $this->set('error', false);
  11.  
  12.         // If a user has submitted form data:
  13.         if (!empty($this->data))
  14.         {
  15.             // First, let's see if there are any users in the database
  16.             // with the username supplied by the user using the form:
  17.  
  18.             $someone = $this->User->findByUsername($this->data['User']['username']);
  19.  
  20.             // At this point, $someone is full of user data, or its empty.
  21.             // Let's compare the form-submitted password with the one in
  22.             // the database.
  23.  
  24.             if(!empty($someone['User']['password']) && $someone['User']['password'] == md5($this->data['User']['password']))
  25.             {
  26.                 // Note: hopefully your password in the DB is hashed,
  27.                 // so your comparison might look more like:
  28.                 // md5($this->data['User']['password']) == ...
  29.  
  30.                 // This means they were the same. We can now build some basic
  31.                 // session information to remember this user as 'logged-in'.
  32.  
  33.                 $this->Session->write('User', $someone['User']);
  34.  
  35.                 // Now that we have them stored in a session, forward them on
  36.                 // to a landing page for the application.
  37.                
  38.                 $path=$this->Session->read('returnPath');
  39.                 if (isset($path) && !empty($path) ){
  40.                     $this->redirect($path);
  41.                     $this->Session->del('returnPath');
  42.                 }else{
  43.                     $this->redirect('/admin/actors/');
  44.                 }   
  45.             }
  46.             // Else, they supplied incorrect data:
  47.             else
  48.             {
  49.                 // Remember the $error var in the view? Let's set that to true:
  50.                 $this->set('error', true);
  51.             }
  52.         }
  53.     }
  54.  
  55.     function logout()
  56.     {
  57.         // Redirect users to this action if they click on a Logout button.
  58.         // All we need to do here is trash the session information:
  59.  
  60.         $this->Session->delete('User');
  61.  
  62.         // And we should probably forward them somewhere, too...
  63.      
  64.         $this->redirect('/');
  65.     }
  66. }
  67. ?>
Parsed in 0.086 seconds, using GeSHi 1.0.7.14

Modify this Paste