CodeIgniter Forums
[Solved] Can't use method return value in write context - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: [Solved] Can't use method return value in write context (/showthread.php?tid=60704)



[Solved] Can't use method return value in write context - El Forum - 06-07-2014

[eluser]riwakawd[/eluser]
Hello I am trying to make custom validation for my login but getting error. Can't use method return value in write context error on line 32

Fatal error: Can't use method return value in write context in C:\xampp\htdocs\codeigniter-cms-1\admin\application\modules\backend\models\user\user_model.php on line 32

Code:
public function index() {
if($this->user_model->isLogged()) {
   redirect('dashboard');
}
   $this->ControllerCommonLogin();
}

public function ControllerCommonLogin() {
    $data['title'] = $this->lang->line('heading_title');
    $data['action'] = site_url('backend/login/validate');
    $this->load->view('template/common/login', $data);
}

protected function validate() {
   if (!empty($this->input->post('username')) || !empty($this->input->post('password') || !$this->user->login($this->input->post('username'), $this->input->post('password')))) {
     $this->error['warning'] = $this->lang->line('error_login'); // Line 32
   }
     return !$this->error;
   }

Model

Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User_model extends CI_Model {

private $user_id;
private $username;

public function __construct() {
parent::__construct();
if(!empty($this->session->userdata('user_id'))) {

$user_query = $this->db->query("SELECT * FROM " . $this->input->post('db_prefix') . "user WHERE user_id = '" .$this->session->userdata('user_id') . "' AND status = '1'");

if($user_query->num_rows > 0) {
$this->user_id = $user_query->row('user_id');
$this->username = $user_query->row('username');
$this->db->query("UPDATE " . $this->input->post('db_prefix') . "user SET ip = '" . $this->input->ip_address()."' WHERE user_id = '" . (int)$this->session->userdata('user_id') . "'");
$user_group_query = $this->db->query("SELECT * FROM " . $this->input->post('db_prefix')  . "user_group WHERE user_group_id = '" . (int)$query->row('user_group_id') . "'");
} else {
$this->logout();
}
}
}


public function login($username, $password) {

$user_query = $this->db->query("SELECT * FROM " .  $this->input->post('db_prefix') . "user WHERE username = '" . $username . "' AND password = '" . $this->bcrypt->hash_password($this->input->post('password')), $password . "' AND status = '1'");

if ($user_query->num_rows) {
$this->session->set_userdata('user_id') = $user_query->row('user_id');

$this->user_id = $user_query->row('user_id');
$this->username = $user_query->row('username');  

return true;
} else {
return false;
}
}

public function logout() {
$this->session->unset_userdata['user_id'];
$this->user_id = '';
$this->username = '';
}

public function isLogged() {
return $this->user_id;
}
}



[Solved] Can't use method return value in write context - El Forum - 06-07-2014

[eluser]Tpojka[/eluser]
What is line 32 in model?


[Solved] Can't use method return value in write context - El Forum - 06-07-2014

[eluser]riwakawd[/eluser]
[quote author="Tpojka" date="1402141687"]What is line 32 in model?[/quote]

$this->error['warning'] = $this->lang->line('error_login'); // Line 32 As show in first code example


[Solved] Can't use method return value in write context - El Forum - 06-07-2014

[eluser]Tpojka[/eluser]
Few things before trying to solve:
1. method has no name;
2. model as represented has no that line

First block of code, if controller, has no name of method.
Error that is displayed tells that something is wrong in model.
And in Model there is no that line, so can you put things together again, please?

Also try to check if post data is opposite to FALSE. Maybe you get something.


[Solved] Can't use method return value in write context - El Forum - 06-07-2014

[eluser]InsiteFX[/eluser]
Code:
// $this->session->set_userdata($newdata); Now look at your code below!

$this->session->set_userdata('user_id') = $user_query->row('user_id');



[Solved] Can't use method return value in write context - El Forum - 06-07-2014

[eluser]riwakawd[/eluser]
[quote author="Tpojka" date="1402146893"]Few things before trying to solve:
1. method has no name;
2. model as represented has no that line

First block of code, if controller, has no name of method.
Error that is displayed tells that something is wrong in model.
And in Model there is no that line, so can you put things together again, please?

Also try to check if post data is opposite to FALSE. Maybe you get something.[/quote]

I have done this below but still get error Fatal error: Can't use method return value in write context in C:\xampp\htdocs\codeigniter-cms-1\admin\application\modules\backend\models\user\user_model.php on line 32

I have hmvc and added MY Form Validation

Code:
public function __construct() {
      parent::__construct();
      $this->load->library('form_validation');
      $this->load->model('backend/user/user_model');
      $this->lang->load('backend/english', 'english');
  $this->lang->load('backend/common/login', 'english');
   }

public function ControllerCommonLogin() {

      $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
      $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_checkinfo');

      $data['title'] = $this->lang->line('heading_title');
      $data['action'] = site_url('backend/login');

      if($this->form_validation->run($this) == FALSE) {
        $this->load->view('template/common/login', $data);
      } else {
        redirect('dashboard', 'refresh');
      }
    
   }


   public function validate($password) {
      $username = $this->input->post('username');

       //query the database
      $result = $this->user_model->login($username, $password);

      if($result) {
        
         $sess_array = array();
         foreach($result as $row){
         $sess_array = array(
         'user_id' => $row->id,
         'username' => $row->username,
         'ip' => $row->ip
         );

         $this->session->set_userdata('isLogged', $sess_array);
      }
         return TRUE;
      } else {
         $this->form_validation->set_message('checkinfo', 'Invalid username or password');
         return false;
      }
   }

And added this library

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Form_validation extends CI_Form_validation {

function run($module = '', $group = '') {
  (is_object($module)) AND $this->CI = &$module;
  return parent::run($group);
}

}



[Solved] Can't use method return value in write context - El Forum - 06-07-2014

[eluser]riwakawd[/eluser]
[quote author="InsiteFX" date="1402147925"]
Code:
// $this->session->set_userdata($newdata); Now look at your code below!

$this->session->set_userdata('user_id') = $user_query->row('user_id');
[/quote]

I think I all most got it just trying now to get it redirect working.

Code:
public function index() {
  $this->ControllerCommonLogin();
   }

   public function ControllerCommonLogin() {

      $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
      $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_checkinfo');

      $data['title'] = $this->lang->line('heading_title');
      $data['action'] = site_url('backend/login');

      if($this->form_validation->run($this) == FALSE) {
        $this->load->view('template/common/login', $data);
      } else {
        redirect('dashboard');
      }
    
   }

   public function validate($password) {
      $username = $this->input->post('username');

       //query the database
      $result = $this->user_model->login($username, $password);

      if($result) {
        
         $sess_array = array();
         foreach($result as $row){
         $sess_array = array(
         'user_id' => $row->id,
         'username' => $row->username,
         'ip' => $row->ip
         );

         $this->session->set_userdata('isLogged', $sess_array);
      }
         return TRUE;
      } else {
         $this->form_validation->set_message('checkinfo', 'Invalid username or password');
         return false;
      }
   }
  
}

Model

Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User_model extends CI_Model {

private $user_id;
private $username;
private $permission = array();

public function __construct() {
  parent::__construct();
  if(!empty($this->session->userdata('user_id'))) {

  $user_query = $this->db->query("SELECT * FROM " . $this->input->post('db_prefix') . "user WHERE user_id = '" .$this->session->userdata('user_id') . "' AND status = '1'");

  if($user_query->num_rows > 0) {
      $this->user_id = $user_query->row('user_id');
      $this->username = $user_query->row('username');
      $this->db->query("UPDATE " . $this->input->post('db_prefix') . "user SET ip = '" . $this->input->ip_address()."' WHERE user_id = '" . (int)$this->session->userdata('user_id') . "'");
      $user_group_query = $this->db->query("SELECT * FROM " . $this->input->post('db_prefix')  . "user_group WHERE user_group_id = '" . (int)$query->row('user_group_id') . "'");
      } else {
         $this->logout();
      }
    }
  }


function login($username, $password){
   $this->db->select('user_id, username, password');
   $this->db->from('user');
   $this->db->where('username', $username);
  $this->db->where('password', $this->bcrypt->hash_password($password));
  $user_query = $this->db >get();
  
   if($user_query->num_rows() == 0) {
   return $user_query->result();
    } else {
    return false;
     }
   }


function logout() {
  $this->session->unset_userdata('user_id');

  $this->user_id = '';
  $this->username = '';
}

function isLogged() {
  return $this->user_id;
}

}



[Solved] Can't use method return value in write context - El Forum - 06-07-2014

[eluser]InsiteFX[/eluser]
You usually get that error in an if statement calling a method that does writes.



[Solved] Can't use method return value in write context - El Forum - 06-07-2014

[eluser]riwakawd[/eluser]
[quote author="InsiteFX" date="1402150272"]You usually get that error in an if statement calling a method that does writes.
[/quote]

I think my code is messed up I am going to start fresh. Thanks all for help and advice.