Codeigniter can't find set_userdata() from model - php

The Session library is autoloaded
My model:
class Login_model extends CI_Model{
function __construct(){
parent::__construct();
}
public function validate(){
$username = $this->security->xss_clean($this->input->post('username'));
$password = $this->security->xss_clean($this->input->post('password'));
$this->db->where('username', $username);
$this->db->where('password', $password);
$query = $this->db->get('users');
if($query->num_rows == 1)
{
$row = $query->row();
$data = array(
'id' => $row->id,
'first_name' => $row->first_name,
'last_name' => $row->last_name,
'username' => $row->username,
'validated' => true
);
$this->session->set_userdata($data); // ### line 28 ###
return true;
}
return false;
}
}
Gives this error:
Fatal error: Call to undefined method Session::set_userdata() in
/var/www/codeIgniterTest/_application/models/login_model.php on line
28

Have you loaded the Session library?
class Login_model extends CI_Model {
function __construct()
{
parent::__construct();
$this->load->library('session');
}

Try debugging and check all the included files by.
$included_files = get_included_files();
foreach ($included_files as $filename) {
echo "$filename\n";
}

I found the problem. I created my own Session class, but forgot to extend the CI_Session.
Thanks for the help!

Related

i can't update database using code igniter

i'm using controller to call update function from model, but when i try to update for column name, that's no working.
---- Controller ----
<?php
defined('BASEPATH') or exit('No direct script access allowed');
class editprofile extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('user');
}
public function index()
{
$this->form_validation->set_rules('email', 'email', 'required|valid_email');
$this->form_validation->set_rules('namalengkap', 'namalengkap', 'required');
$this->form_validation->set_rules('password', 'PASSWORD', 'required');
if ($this->form_validation->run() == false) {
$this->load->view('v_profile');
} else {
$row = $this->session->userdata('id');
$data = array(
'name' => $this->input->post('name'),
'email' => $this->input->post('email'),
'password' => $this->input->post('password'),
'phone' => $this->input->post('phone'),
'address' => $this->input->post('address'),
'kecamatan' => $this->input->post('kecamatan'),
'kelurahan' => $this->input->post('kelurahan'),
'rt' => $this->input->post('rt'),
'rw' => $this->input->post('rw'),
'zip code' => $this->input->post('zip code'),
'city' => $this->input->post('city'),
'province' => $this->input->post('province'),
);
$result = $this->user->update($data, $row);
if ($result > 0) {
$this->updateProfil();
$this->session->set_flashdata('msg', show_succ_msg('Data Profile Berhasil diubah, silakan lakukan login ulang!'));
redirect('c_login/profile');
} else {
$this->session->set_flashdata('msg', show_err_msg('Data Profile Gagal diubah'));
redirect('c_login/profile');
}
}
}
}
----- model -----
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class User extends CI_Model
{
private $uid = 'id';
private $tabel = 'users';
public function __construct()
{
parent::__construct();
}
public function login($email, $password)
{
$this->db->where('email', $email);
$this->db->where('password', $password);
return $this->db->get($this->tabel);
}
function update($data, $id)
{
$this->db->where($this->uid, $id);
$this->db->update($this->tabel, $data);
}
public function get_by_cookie($kue)
{
$this->db->where('cookie', $kue);
return $this->db->get($this->tabel);
}
}
----- Core / MY_Controller -----
<?php
defined('BASEPATH') or exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('user');
}
public function updateProfil()
{
if ($this->userdata != '') {
$data = $this->user->select($this->userdata->email);
$this->session->set_userdata('userdata', $data);
$this->userdata = $this->session->userdata('userdata');
}
}
}
In your update function you have not set any values. As set method required array values you should give if. Good practice is using set before update and finally update of table. Follow the following codes for updating your users table.
function update($data, $id)
{
$this->db->set($data);
$this->db->where($this->uid, $id);
$this->db->update($this->tabel);
}
use this code. I hope it will work well.
in your controller
$result = $this->user->update($data, $row);
variable data is array
so in your model:
function update($data=array(), $id){
$this->db->where($this->uid, $id);
$this->db->update($this->tabel, $data);
}

Undefined variable: result in codeigniter

I am trying to get to data from database details from model to controller i was not get
it's saying Undefined variable: result can any one know how to get data from model to controller?
controller.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Dashboard extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('login_model');
$this->load->helper(array('form', 'url'));
$this->load->library('session');
}
public function setting()
{
$this->load->database();
session_start();
$userss="";
if($_SESSION["registerid"])
{
$userss = $_SESSION["registerid"];
}
$this->load->model('setting_model');
$this->setting_model->getsetting($userss);
$data12345 = array(
'registerid' => $result[0]->registerid,
'username' => $result[1]->username,
'email' => $result[2]->email,
'password' => $result[3]->password,
'companyname' => $result[0]->companyname,
'phonenumber' => $result[0]->phonenumber ,
'address' => $result[0]->address,
'postalcode' => $result[0]->postalcode,
'descriptionofcompany' =>$result[0]->descriptionofcompany,
'creditcardinformation' =>$result[0]->creditcardinformation,
'title'=>'Setting Page'
);
//passing page title
$this->load->helper('url'); // passing base url
$this->load->view('setting',$data12345); // passing setting.php view
}
}
setting_model.php
<?php
class setting_model extends CI_Model{
function __construct() {
parent::__construct();
}
public function getsetting($userss) {
$this->load->database();
$condition = "username =" . "'" . $userss . "'";
$this->db->select('*');
$this->db->from('register');
$this->db->where($condition);
$this->db->limit(1);
$query = $this->db->get();
if ($query->num_rows() == 1) {
return $query->result();
} else {
return false;
}
}
// Read data from database to show data in admin page
}//end class
?>
Looks like you forgot to assign the query result to the $result variable. Try this $result = $this->setting_model->getsetting($userss); in the controller.

Call to a member function where() on a non-object in ... on line 13

I am new user of CodeIgniter, I am trying to create simple login page using CodeIgniter but I am receiving the following error:
Call to a member function where() on a non-object in C:\xampp\htdocs\CodeIg\application\models\user.php on line 13
I am uncertain as to where to begin debugging this, suggestions would be appreciated.
My code for the model is as follows:
<?php
class User extends CI_model{
function __construct()
{
parent::__construct();
}
public function verifyuser()
{
$username = $this->input->post('username');
$password = $this->input->post('password');
$remember = $this->input->post('remember');
$this->db->where('username', $username);
$this->db->where('password', $password);
$query = $this->db->get('user_login');
$result = array();
if($query->num_rows==0)
{
$result['false']=false;
return $result;
}
else
{
$result=$query->result();
foreach($result as $item)
{
$result['id']=$item->id;
$result['username']=$item->username;
$result['password']=$item->password;
}
return $result;
}
}
}
?>
and code for controller is:
<?php
class User_Controller extends CI_controller
{
public function getloginData()
{
$this->load->model('User');
$rvalue = $this->User->verifyuser();
header('Content-type: application/json');
echo json_encode($rvalue);
}
}
?>
Database is not initialized.
You need to include the "database" in your application/config/autoload.php:
$autoload['libraries'] = array('database', 'session');
Or in your model class constructor:
class User extends CI_model{
public function __construct()
{
parent::__construct();
$this->load->database();
}
}
You can get more information here: http://ellislab.com/codeigniter/user_guide/database/connecting.html
$this->db isn't initialized, so $this->db is null and calling where() on null results in this error.. you have to assign something to $this->db first.
To do this, you have to laod the Model within your controller

Custom Session Handler on CakePHP 2.0.2

i tried follow from http://book.cakephp.org/2.0/en/development/sessions.html#creating-a-custom-session-handler then i cant solve this and im really confuse about this :(
become like this:
<?php
App::uses('DatabaseSession', 'Model/Datasource/Session');
class CartSession implements CakeSessionHandlerInterface {
public $cacheKey;
public function __construct() {
$this->cacheKey = Configure::read('Session.handler.cache');
parent::__construct();
}
// read data from the session.
public function read($id) {
$result = Cache::read($id, $this->cacheKey);
if ($result) {
return $result;
}
return parent::read($id);
}
// write data into the session.
public function write($id, $data) {
$result = Cache::write($id, $data, $this->cacheKey);
if ($result) {
return parent::write($id, $data);
}
return false;
}
// destroy a session.
public function destroy($id) {
$result = Cache::delete($id, $this->cacheKey);
if ($result) {
return parent::destroy($id);
}
return false;
}
// removes expired sessions.
public function gc($expires = null) {
return Cache::gc($this->cacheKey) && parent::gc($expires);
}
}
?>
Output Error:
Fatal error: Class CartSession contains 2 abstract methods and must therefore be declared abstract or implement the remaining methods (CakeSessionHandlerInterface::open, CakeSessionHandlerInterface::close) in /Users/user/Sites/app/shop/Model/Datasource/Session/CartSession.php on line 43
I did added in core.php:
Configure::write('Session', array(
'defaults' => 'database',
'handler' => array(
'engine' => 'CartSession',
'model' => 'Session',
'cache' => 'apc'
)
));
Cache::config('apc', array('Engine' => 'Apc'));
You need to extend DatabaseSession. So your class declaration will look like:
class CartSession extends DatabaseSession implements CakeSessionHandlerInterface

Codeigniter issues when loading Model

I am new to codeigniter and I am having some issues loading a model in my constructor method. Could someone help me out? Below is the code from the controller I am trying to load the model from...
<?php
class Login extends CI_Controller {
function Login(){
$this->load->model('membership_model');
}
public function index(){
$this->load->view('login_view.php');
}
public function authenticate(){
$user = $this->input->post('username');
$pass = sha1($this->input->post('password'));
if($user != null && $pass != null){
$access = $this->membership_model->request_access($user, $pass);
if($access == true){
$cookie = array(
'name' => 'username',
'value' => $user,
'expire' => '86500',
'domain' => 'unleashourmedia.com',
'path' => '/',
'prefix' => '',
'secure' => TRUE
);
$this->input->set_cookie($cookie);
echo "cookie";
} else {
redirect('login');
}
}
}
}
?>
The problem is you are not calling the constructor of the parent class.
Add this as the first line in your constructor:
parent::__construct();
function Login(){
$this->load->model("membership_model","",TRUE);
}
//make sure you call parent constructor before anything in that constructor like this
function Login(){
parent::__construct();
$this->load->model('membership_model');
}
//and you may also try to name the constructor __construct
function __construct(){
parent::__construct();
$this->load->model('membership_model');
}

Categories