简体   繁体   中英

Warnin:Illegal offset type [CORE\Cake\Model\Model.php, line 2734] came on click of login in PHP

I created a simple login form using Cake Php.When i click on the Login button one Warning appear

Illegal offset type [CORE\Cake\Model\Model.php, line 2734

I have use link as a reference http://bakery.cakephp.org/articles/SeanCallan/2007/04/17/simple-form-authentication-in-1-2-xx

Code of Login.ctp

Login

<?php echo $this->Form->create('User', array('action' => 'login'));?>
    <?php echo $this->Form->input('username');?>
    <?php echo $this->Form->input('password');?>
    <?php echo $this->Form->submit('Login');?>
<?php echo $this->Form->end(); ?>

2. modal file

find(array('username' => $data['username'], 'password' => md5($data['password'])), array('id', 'username')); if(empty($user) == false) return $user['User']; return false; } } ?>

3.controller file

 <?php App::uses('AppController', 'Controller'); class UsersController extends AppController { var $name = "Users"; var $helpers = array('Html', 'Form'); var $components = array("Auth"); function index() { } function beforeFilter() { $this->__validateLoginStatus(); } function login() { if(empty($this->data) == false) { if(($user = $this->User->validateLogin($this->data['User'])) == true) { $this->Session->write('User', $user); $this->Session->setFlash('You\\'ve successfully logged in.'); $this->redirect('index'); exit(); } else { $this->Session->setFlash('Sorry, the information you\\'ve entered is incorrect.'); exit(); } } } function logout() { $this->Session->destroy('user'); $this->Session->setFlash('You\\'ve successfully logged out.'); $this->redirect('login'); } function __validateLoginStatus() { if($this->action != 'login' && $this->action != 'logout') { if($this->Session->check('User') == false) { $this->redirect('login'); $this->Session->setFlash('The URL you\\'ve followed requires you login.'); } } } } ?> 

Why that happened.i am new in this.Some developers in PHP chat room suggest me to not use that Cake PHP. Any help is appreciated

Illegal offset type means that you tried to use an object or an array to index an array:

$x = stdClass;
$a = array(1,2,3,4);
$a[$x]; // illegal offset type

Check your code for possible places where you've taken user input (which could be an array of values even though you thought it's just one value) and used it in some function that expected just a value.

If that Cake internal function expected a value and tried to use it as the offset of an array, this message would appear.

Obs

The only place I notice in your code that passes a parameter (and not literal string values) is here:

$this->User->validateLogin($this->data['User']))

Do a var_dump on $this->data['User'] and see what's in it perhaps it's an array and you should've extracted just $this->data['User']['id'] I don't know, i haven't played that much with Cake.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM