Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Login CakePHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 277
    Comment on it

    We can create a login function for logging in using cakePHP.

    The login functionality can be added using the Auth component available in the cakePHP.

    You do not need to create the session or check for encrypted password in the database as Auth component does it.

    You need to create a function in your controller.

    In this the Auth will check for the value to match in the database and redirect to the edit page.
     

    public function login()
        {
            if ($this->request->is('post')) {
              
    
    if ($this->Auth->login()) {
    
    $this->redirect(array('controller'=>'users','action'=>'edit',$this->Auth->user('id')));
    
    } else {
                    $this->Session->setFlash("Invalid Credentials");
                    $this->redirect(array('controller'=>'users','action'=>'login'));
    
         }
    
        }
    
    }

    And we also have to include the Auth component in the AppController.
     

    class AppController extends Controller {
    
        public $components = array(
        'Auth' => array(
            'authenticate' => array(
                'Form' => array(
                    'passwordHasher' => array(
                        'className' => 'Simple'
                    )
                )
            ),
            'loginRedirect' => array(
                    'controller' => 'users',
                     'action' => 'contact'
                     ),
                 'logoutRedirect' => array(
                     'controller' => 'users',
                     'action' => 'login'
                     )
        )
    );

    And at last we have to create a logout function.

    public function logout(){
        return $this->redirect($this->Auth->logout());
    }

    And you login function is created.

    Now you also need a ctp file where the email and the password will be entered.
    <ul class="login">
                    
                        

    <?php echo $this->Session->flash(); ?>
                    
                    <li>
                        <?php echo $this->Form->input('email', array('type' => 'text', 'placeholder' => 'Enter Username')); ?>
                    </li>
                    <li>
                        <?php echo $this->Form->input('password', array('type' => 'password', 'placeholder' => 'Enter Password')); ?>
                    </li>
    
    <li>
                        <?php echo $this->Form->submit('LOGIN', array('class' => 'btnNew', 'type' => 'submit')); ?>
                       
                    </li>
    
    <?php echo $this->Form->end(); ?>

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: