Monday, December 29, 2008

Re: Newby: "Undefined index" error

First you probably want the form name to be login.ctp, since that's
what you're trying to do.

My views/users/login.ctp looks like:

<?php
if ($session->check('Message.auth')) $session->flash('auth');
echo $form->create('User', array('action' => 'login'));
echo $form->input('user_name');
echo $form->input('user_password', array('type' => 'password'));
echo $form->end('Login');

echo $html->link('Register',array
('controller'=>'users','action'=>'register'));

?>

Then in my users_controller.php, I have a beforeFilter to allow access
to forms that need to be accessed before login:

function beforeFilter() {
parent::beforeFilter();

$this->Auth->allow
('login','logout','recaptcha','register', 'sign_up');
}

And then my login and logout functions have a bit more code (although
the empty login should work) to make sure I redirect afterwards:

/**
* Login function - this is where we do the login ...
*/
function login() {
if ($this->Auth->user())
{
$auth_user = $this->Auth->user();
$this->set('auth_user',$auth_user);
$this->Session->SetFlash(__('Successfully logged in as '.
$auth_user['User']['full_name'],true));
$this->redirect($this->Auth->redirect());
}
}

/**
* Logout function
*/
function logout() {

if ($this->Auth->logout()){
$this->Session->SetFlash(__('Successfully logged
out',true));

} else {
$this->Session->SetFlash(__('Weird - logout failed - were
you logged in ?',true));
}
$this->redirect($this->referer());
}


On Dec 29, 2:56 am, danwednesday <d...@iomi.net> wrote:
> Thanks gearvOsh,
>
> Adding that snippet to the controller caused an error, so I read
> through the documentation link you sent through, and that has resolved
> the errors.  However, now, it's just not working.  When I submit a
> username and password, I just get redirected back to the form, with
> the username truncated to just 1 letter.  Regardless of what I input
> to the form (correct or incorrect) it just does the same thing, and
> doesn't let me in to the rest of the admin system.
>
> The code I have in the 'controllers/users_controller.php' file is:
>
> <?php
> class UsersController extends AppController {
>
>     var $name = 'Users';
>     var $components = array('Auth'); // Not necessary if declared in
> your app controller
>
>     /**
>      *  The AuthComponent provides the needed functionality
>      *  for login, so you can leave this function blank.
>      */
>     function login() {
>     }
>
>     function logout() {
>         $this->redirect($this->Auth->logout());
>     }}
>
> ?>
>
> The code in the views/users/index.ctp file is:
>
> <?php
>     if  ($session->check('Message.auth')) $session->flash('auth');
>     echo $form->create('User', array('action' => 'login'));
>     echo $form->input('username');
>     echo $form->input('password');
>     echo $form->end('Login');
> ?>
>
> Totally confused!  Can someone point me in the right direction to get
> this simple thing working?
>
> Thanks all...
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: