[CakePHP]: Create function login in cakephp 2.x


Posted on 2015-07-25 15:48:05


Today I will introduce to guys how to create function login for user in cakephp framework. 1. You could create function checklogin for all users go to back end site.
/* Check user is login or not */ function checkLogin() { $userInfo = $this->Session->read('UserInfo'); if(isset($userInfo)) { $this->curUser = $userInfo; } else { $authToken = $this->Cookie->read('UserAuthToken'); if(isset($authToken)) { $user = $this->User->find('first',array('conditions'=>array('User.auth_token'=>$authToken))); if(!empty($user)) { $this->curUser = $user['User']; } else { $this->Session->write('RedirectUrl',$_SERVER['REDIRECT_URL']); $this->redirect(array('controller'=>'users','action'=>'login','admin'=>0)); } } else { $this->Session->write('RedirectUrl',$_SERVER['REDIRECT_URL']); $this->redirect(array('controller'=>'users','action'=>'login','admin'=>0)); } } // => Is Login $this->getTopMenu(); }  
2. Function login  
function login() { Configure::write('Config.language', 'eng'); // Check is login => redirect $authToken = $this->Session->read('UserAuthToken'); if(isset($authToken)) { // redirect //$this->login_redirect($user); } // ---------------- $this->layout = null; if($this->request->is('post')) { $email = $this->request->data['User']['email']; $password = Security::hash($this->request->data['User']['password'], 'sha1', true); $user = $this->User->find('first',array('conditions'=>array('User.email'=>$email,'User.password'=>$password))); if(!empty($user)) { if($user['User']['active']) { // Create login key $loginTime = date("Y-m-d H:i:s"); $authToken = $user['User']['id'] . "user_key" . $loginTime; $authToken = sha1($authToken); // ---------------- // Check first login $firstLogin = (empty($user['User']['auth_token'])) ? true : false; // update login info $this->User->updateAll(array('User.auth_token'=>"'$authToken'"),array('User.id'=>$user['User']['id'])); // if 'remember me' checked, save cookie if(isset($this->request->data['User']['remember'])) { $this->Cookie->write('UserAuthToken', $authToken, null, '30 days'); } else { $this->Cookie->delete('UserAuthToken'); } $this->Session->write('UserInfo', $user['User']); // Redirect to previous page $redirectUrl = $this->Session->read('RedirectUrl'); if(isset($redirectUrl)) { $url = 'http://'.$_SERVER['HTTP_HOST'].$redirectUrl; $this->Session->delete('RedirectUrl'); $this->redirect($url); } // Redirect part if($firstLogin) { $this->flashSuccess(__('This is your first login to administrator page, please change your default password.')); $this->redirect(array('controller'=>'users','action'=>'password','admin'=>true)); } // Redirect base on group if($user['User']['group'] == GROUP_ADMIN) { $this->redirect(array('controller'=>'users','action'=>'dashboard','admin'=>true)); } else { $this->redirect(array('controller'=>'users','action'=>'dashboard','admin'=>true)); //debug('Config redirect here!'); } } else { $this->flashError(__('This staff is currently disabled')); $this->redirect($this->referer()); } } else { $this->flashError(__('Cannot login the system')); $this->redirect($this->referer()); } } }  
3. Function logout  
function logout() { $this->Session->delete('UserInfo'); $this->Cookie->delete('UserAuthToken'); $this->redirect(array('controller'=>'users','action'=>'login','admin'=>0)); }  
4. Form login in file login.ctp  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="pl" xml:lang="pl"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title><?php echo __('Login') ?></title> <?php echo $this->Html->css('login.css'); ?> </head> <body> <div class="wrap"> <div id="content"> <div id="main"> <?php echo $this->Html->image('admin/logoFix.png'); ?> <div class="full_w"> <?php echo $this->Session->flash(); ?> <?php echo $this->Form->create('User'); ?> <label for="login"><?php echo "Email" ?>:</label> <?php echo $this->Form->input('email',array('label'=>'','div'=>'','size'=>30,'class'=>'text'));?> <label for="pass"><?php echo "Password" ?>:</label> <?php echo $this->Form->input('password',array('label'=>'','div'=>'','size'=>30,'class'=>'text'));?> <label for="rem" style="display: inline;"><?php echo "Remember" ?></label> <input type="checkbox" name="data[User][remember]" /> <div style="float: right"> <?php //echo $this->Html->link(__('Forgot Password').'?',array('action'=>'forgot_password')); ?> </div> <div class="sep"></div> <button type="submit" class="ok"><?php echo "Login" ?></button> <?php echo $this->Form->end(); ?> </div> <?php echo $this->element('admin/footer') ?> </div> </div> </div> </body> </html>  
I hope This article will help you for first times to work with cakephp. You can see more guides for cakephp at http://tutscode.com/category/frame-work/cakephp-mvc More knowledge for php at http://tutscode.com/category/programming-language/php-programming-language