06.28 php saved
ddd
Tags add more
 
Note
ddd
  1. <h1>User Login</h1>
  2. <table class="whatever">
  3.         <tr>
  4.                 <th>Username</th>
  5.                 <td><?php echo $form->input('username'); ?></td>
  6.         </tr>
  7.         <tr>
  8.                 <th>Password</th>
  9.                 <td><?php echo $form->input('password'); ?></td>
  10.         </tr>
  11.   <tr>
  12.                 <th>Remember Me</th>
  13.                 <td><?php echo $form->input('remember_me', array('label' =>
  14. 'Remember Me', 'type' => 'checkbox')); ?></td>
  15.         </tr>
  16.         <tr>
  17.                 <th> </th>
  18.                 <th align="right"><?php echo $form->submit('/images/login.png',
  19. array('srcover' => $this->base . '/images/login-over.png', 'srcdown'
  20. => $this->base . '/images/login-down.png', 'border' => '0'));?></th>
  21.         </tr>
  22. </table>
  23.  
  24. <input type="hidden" name="XDEBUG_SESSION_START" value="tim" />
  25. <?php echo $form->end(); ?>
  26.  
  27. *** /views/users/register.ctp
  28.  
  29. <h1>User Registration</h1>
  30.  
  31. <?php echo $form->create('User', array('action' => 'register'));?>
  32.  
  33. <table class="whatever">
  34.   <tr>
  35.     <th>Username</th>
  36.     <td><?php echo $form->input('username',
  37. array_merge($errorMessages, array('size'=>'20', 'div' => null))); ?
  38. >  <span class="required_field">*</span></td>
  39.  
  40.   </tr>
  41.   <tr>
  42.     <th>Email Address</th>
  43.     <td><?php echo $form->input('email', array_merge($errorMessages,
  44. array('size'=>'30', 'div' => null))); ?>  <span
  45. class="required_field">*</span><br />
  46.     Please note your confirmation email will be sent to this address</
  47. td>
  48.   </tr>
  49.   <tr>
  50.     <th>Password</th>
  51.     <td><?php echo $form->input('password', array('type'=>'password',
  52. 'size'=>'15', 'div' => null)); ?>  <span
  53. class="required_field">*</span></td>
  54.   </tr>
  55.   <tr>
  56.     <th>Confirm Password</th>
  57.     <td><?php echo $form->input('password2', array('type'=>'password',
  58. 'size'=>'15', 'div' => null)); ?>  <span
  59. class="required_field">*</span></td>
  60.   </tr>
  61.   <tr>
  62.     <th> </th>
  63.     <th align="right"><?php echo $form->submit('/images/register.png',
  64. array('srcover' => $this->base . '/images/register-over.png',
  65. 'srcdown' => $this->base . '/images/register-down.png', 'border' =>
  66. '0'));?></th>
  67.    </tr>
  68. </table>
  69.  
  70. <input type="hidden" name="XDEBUG_SESSION_START" value="tim" />
  71. <?php echo $form->end(); ?>
  72.  
  73. *** /controllers/login.php
  74.  
  75. <?php
  76. /**
  77. * User Controller class
  78. */
  79. class UsersController extends AppController {
  80.  
  81.         var $name = 'Users';
  82.         var $helpers = array('Html', 'Form', 'Session', 'Misc');
  83.         var $components = array('Auth', 'Email', 'Cookie');
  84.   var $uses = array('User', 'UserView');
  85.  
  86.   function index() {
  87.     $this->redirect('/home');
  88.   }
  89.  
  90.   /**
  91.   * Login the user
  92.   */
  93.   function login() {
  94.     //-- code inside this function will execute only when autoRedirect
  95. was set to false (i.e. in a beforeFilter).
  96.  
  97.     if ($this->RequestHandler->isPost()) {
  98.       if ($this->Auth->user()) {      // Does user/password checking
  99.         if (!empty($this->data) && $this->data['User']['remember_me']
  100. == '1') {
  101.           // Save cookie only if checkbox ticked
  102.           $cookie = array();
  103.           $cookie['username'] = $this->data['User']['username'];
  104.           $cookie['password'] = $this->data['User']['password'];
  105.           $this->Cookie->write('Auth.User', $cookie, true, '+2
  106. weeks');
  107.           unset($this->data['User']['remember_me']);
  108.         }
  109.  
  110.         $this->redirect($this->Auth->redirect());
  111.       } else {
  112.         $this->Session->setFlash('Invalid user or password');
  113.       }
  114.     }
  115.   }
  116.  
  117.   /**
  118.          * Log out user
  119.          *
  120.          */
  121.         function logout(){
  122.     $cookie = $this->Cookie->read('Auth.User');
  123.     $this->Cookie->del('Auth.User');
  124.  
  125.           $this->redirect($this->Auth->logout());
  126.   }
  127.  
  128.         function register() {
  129.     // Check if user already logged in
  130.     $i = $this->getUserID();
  131.     if ($i != null && $i > 0) {
  132.       $this->Session->setFlash('You\'re already logged in!');
  133.       $this->redirect('/home/index');
  134.     }
  135.  
  136.     // Setup data for page
  137.     $this->set('title', $this->appName .'User Registration');
  138.     $this->set('errorMessages', array('error' =>
  139.                           array('username_size' => 'Your username must
  140. be between 3 and 10 characters long',
  141.                                 'password' => 'Your password must be
  142. between 4 and 10 characters long',
  143.                                 'email' => 'Please enter a valid email
  144. address',
  145.                                 'first_name' => 'name size is too
  146. short'
  147.                 )));
  148.  
  149.                 if (empty($this->data)) {
  150.                         // Just show the form
  151.                 } else {
  152.       // Need to encode password2 so it can be matched with password
  153. by validation, but need to keep the length
  154.       // around to check as encoded password is always the same length
  155.       $this->data['User']['password_length'] = strlen($this-
  156. >data['User']['password2']);
  157.  
  158.       $this->data['User']['password2'] = $this->Auth->password($this-
  159.  
  160. >data['User']['password2']);
  161.  
  162.       // Attempt to register the user
  163.       if ($this->User->save($this->data)) {
  164.           $this->set('title', $this->appName .'User Registration
  165. Succesful');
  166.           $this->set('user', $this->data);
  167.           $this->render('register_email_sent');
  168.           return;
  169.       } else {
  170.         $this->Session->setFlash('Please correct the errors
  171. highlighted below');
  172.         $this->data['User']['password'] = $this->data['User']
  173. ['password2'] = '';
  174.       }
  175.     }
  176.   }
  177.  
  178.   function beforeFilter() {
  179.     $this->Auth->userScope = array('User.state_id' => '12');
  180.     $this->set('title', $this->appName .'User Login');
  181.  
  182.     parent::beforeFilter();
  183.   }
  184. }
  185.  
  186. ?>
  187.  
  188. /controllers/app_controller.php
  189.  
  190. <?php
  191. /**
  192. * Main App Controller File
  193. */
  194. class AppController extends Controller {
  195.         var $components = array('Auth','Cookie', 'Email');
  196.   var $helpers = array('Html', 'Javascript', 'Ajax');
  197.  
  198.   var $home = '/home/view/';
  199.  
  200.   /**
  201.     * Load the Authentication
  202.     *
  203.     * @access public
  204.     */
  205.   function beforeFilter(){
  206.     $this->Auth->loginAction = array('controller' => 'users', 'action'
  207. => 'login');
  208.     $this->Auth->loginRedirect = array('controller' => 'home');
  209.     $this->Auth->allow('index');
  210.     $this->Auth->authorize = 'controller';
  211.  
  212.     $this->Auth->autoRedirect = false;
  213.  
  214.     if ($this->getUserID() == -1) {
  215.       $this->loginFromCookie();
  216.     }
  217.   }
  218.  
  219.   function loginFromCookie() {
  220.    $cookie = $this->Cookie->read('Auth.User');
  221.    if (!is_null($cookie)) {
  222.      if ($this->Auth->login($cookie)) {
  223.        //  Clear auth message, just in case we use it.
  224.        $this->Session->del('Message.auth');
  225.        //$this->redirect($this->Auth->redirect());
  226.      } else { // Delete invalid Cookie
  227.        $this->Cookie->del('Auth.User');
  228.      }
  229.    }
  230.   }
  231.  
  232.   function getUserID() {
  233.     $usr = $this->Auth->user();
  234.     if (!empty($usr)) {
  235.       return $usr['User']['id'];
  236.     } else {
  237.       return -1;
  238.     }
  239.   }
  240.  
  241. }
Parsed in 0.473 seconds, using GeSHi 1.0.7.14

Modify this Paste