Message: Undefined property: Users::$form_validation - php

im currently trying to change my application from MySQL database to MongoDB database. My project requires to compare two of these, and my MySQL code was working and now it gives me this error.
Stack Trace:
A PHP Error was encountered Severity: Notice Message: Undefined property: Users::$form_validation Filename: controllers/users.php Line Number: 26
Error line:
$this->form_validation->set_rules('username','Username','trim|required|min_length[4]|xss_clean');
Code:
<?php
class Users extends CI_Controller{
public function register(){
$this->form_validation->set_rules('first_name','First Name','trim|required|max_length[50]|min_length[2]|xss_clean');
$this->form_validation->set_rules('last_name','Last Name','trim|required|max_length[50]|min_length[2]|xss_clean');
$this->form_validation->set_rules('email','Email','trim|required|max_length[100]|min_length[5]|xss_clean|valid_email');
$this->form_validation->set_rules('username','Username','trim|required|max_length20]|min_length[6]|xss_clean');
$this->form_validation->set_rules('password','Password','trim|required|max_length[20]|min_length[6]|xss_clean');
$this->form_validation->set_rules('password2','Confirm Password','trim|required|max_length[50]|min_length[2]|xss_clean|matches[password]');
if($this->form_validation->run() == FALSE){
$data['main_content']='users/register';
$this->load->view('layouts/main',$data);
}else{
if($this->User_model->create_member()){
$this->session->set_flashdata('registered','Registered successfully');
redirect('home/index');
}
}
}
public function login(){
//$this->load->model('user_model');
$this->form_validation->set_rules('username','Username','trim|required|min_length[4]|xss_clean');
$this->form_validation->set_rules('password','Password','trim|required|min_length[4]|max_length[50]|xss_clean');
$collection = $this->database->users;
if($this->form_validation->run() == FALSE){
$this->session->set_flashdata('login_failed', 'Sorry, this username doesn't exist.');
redirect('home/index');
} else {
$username=$_POST['login'];
$password=$_POST['password'];
$user_id = $collection->findOne(array('username' => $username, 'password' => md5($password)));
// $username = $this->input->post('username');
// $password = $this->input->post('password');
$user_id = $this->User_model->login_user($username,$password);
if($user_id){
$user_data = array(
'user_id' => $user_id,
'username' => $username,
'logged_in' => true
);
$this->session->set_userdata($user_data);
redirect('home/index');
} else {
$this->session->set_flashdata('login_failed', 'Sorry, this username doesn't exist.');
redirect('home/index');
}
}
}
public function logout(){
$this->session->unset_userdata('logged_in');
$this->session->unset_userdata('user_id');
$this->session->unset_userdata('username');
$this->session->sess_destroy();
redirect('home/index');
}
}

Changing config/autoload.php worked for me, just add form_validation in autoload :
$autoload['libraries'] = array('form_validation');

This because of failed to load relevant library
Method 01
public function __construct()
{
parent::__construct();
$this->load->library('form_validation');
$this->load->helper('form');
}
Method 02
in config/autoload.php
$autoload['libraries'] = array('form_validation');
$autoload['helper'] = array('url', 'form');
Form_Validation Codeigniter

Follow this method-
public function __construct()
{
parent::__construct();
$this->load->library('form_validation');
$this->load->helper('form');
}

Related

Undefined property error when using model

I'm a newbie using Codeigniter,
I have a login controller and a login model in CI, why do I get this error?
"Undefined property : Login::$Login_model"
Error in Line 44 in "login" Controller:
if ($this->Login_model->check_user($username, $password) == TRUE)
This is my "login" Controller.
<?php
class Login extends CI_Controller {
public function __login()
{
parent::__construct();
$this->load->model('Login_model', '', TRUE);
}
function index()
{
if ($this->session->userdata('login') == TRUE)
{
redirect('home');
}
else
{
$this->load->view('login/login_view');
}
}
function process_login()
{
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
if ($this->form_validation->run() == TRUE)
{
$username = $this->input->post('username');
$password = $this->input->post('password');
if ($this->Login_model->check_user($username, $password) == TRUE)
{
//$datalevel = $this->Login_model->check_user($username);
$data = array('username' => $username, 'login' => TRUE);
$this->session->set_userdata($data);
redirect('home');
}
else
{
$this->session->set_flashdata('message', 'Username dan/atau password Anda salah');
redirect('login/index');
}
}
else
{
$this->load->view('login/login_view');
}
}
function process_logout()
{
$this->session->sess_destroy();
redirect('login', 'refresh');
}
And This is my "login_model" model
<?php
class Login_model extends CI_Model {
function Login_model()
{
parent::__construct();
}
var $table = 'user';
function check_user($username, $password)
{
$query = $this->db->get_where($this->table, array('username' => $username, 'password' => $password), 1, 0);
if ($query->num_rows() > 0)
{
return TRUE;
}
else
{
return FALSE;
}
}
}
It says that your Model is not loaded. You can fix this by changing name of below method
public function __login()
to
public function __construct()
This way it will be automatically called when your class is called and your Model will be automatically loaded.

Codeigniter need to connect from one module to another

I want to connect from one controller in one module to another controller in another module. I want to go from my login module to my dashboard module and use a function inside the dashboard module. Just basically switch from my login module to my dashboard module. Here is my login controllers and my dashboard controller.
class Login extends MX_Controller {
function index()
{
$this->load->view('includes/header');
$this->load->view('login_form');
$this->load->view('includes/footer');
}
function validate_credentials()
{
$this->load->model('membership_model');
$query = $this->membership_model->validate();
if($query) // if the user's credentials validated...
{
$data = array(
'username' => $this->input->post('username'),
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('login/site/members_area');
}
else // incorrect username or password
{
$this->index();
}
}
function signup()
{
//$data['main_content'] = 'signup_form';
//$this->load->view('includes/template', $data);
$this->load->view('includes/header');
$this->load->view('signup_form');
$this->load->view('includes/footer');
}
function create_member()
{
$this->load->library('form_validation');
// field name, error message, validation rules
$this->form_validation->set_rules('first_name', 'Name', 'trim|required');
$this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|callback_check_if_email_exists');
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]|max_length[15]|callback_check_if_username_exists');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
$this->form_validation->set_rules('password2', 'Password Confirmation', 'trim|required|matches[password]');
if($this->form_validation->run() == FALSE) // didn't validate
{
$this->load->view('includes/header');
$this->load->view('signup_form');
$this->load->view('includes/footer');
}
else
{
$this->load->model('membership_model');
if($query = $this->membership_model->create_member())
{
$data['account created'] = 'Your account has been created. <br /> <br /> You may now login';
$this->load->view('includes/header');
$this->load->view('signup_successful', $data);
$this->load->view('includes/footer');
}
else
{
$this->load->view('includes/header');
$this->load->view('signup_form');
$this->load->view('includes/footer');
}
}
}
function check_if_username_exists($requested_username)
{
$this->load->model('membership_model');
$username_available = $this->membership_model->check_if_username_exists($requested_username);
if ($username_available)
{
return TRUE;
}else{
return FALSE;
}
}
function check_if_email_exists($requested_email)
{
$this->load->model('membership_model');
$email_available= $this->membership_model->check_if_email_exists($requested_email);
if ($email_available)
{
return TRUE;
}else{
return FALSE;
}
}
function logout()
{
$this->session->sess_destroy();
$this->index();
}
}
my second login controller
class Site extends MX_Controller {
public function __construct()
{
parent::__construct();
$this->is_logged_in();
$this->lang->load('login/login/', 'english');
}
function members_area()
{
$this->load->view('logged_in_area');
//$this->load->module('dashboard/dashboard');
//$this->load->view('home');
}
function is_logged_in()
{
$is_logged_in = $this->session->userdata('is_logged_in');
if(!isset($is_logged_in) || $is_logged_in != true)
{
echo 'You don\'t have permission to access this page. Login';
die();
//$this->load->view('login_form');
}
}
}
the controller in my dashboard module has a function called home I am trying to connect to and use. And the home function of the dashboard has a connection to another module but I cannot get the connection from login to dashboard to work. Also the way my login works I need to connect to the dashboard module through my members_area function. All help greatly appreciated.
Assuming you're using this library
From controller you can use Modules::load or $this->load-module
$moduleInstance = $this->load->module('yourmodule');
// or
$moduleInstance = Modules::load('yourmodule');
// Now you can call any public module controller method
$moduleInstance->somePublicMethod($arg1, $argn);
// you can also use $this->yourmodule as a model instance
Hope this helps

CodeIgniter, Call to a member function post() on a non-object

i am new in CodeIgniter (v.2.1.4). I have a problem with getting post from model.
I have a contoller:
class Login extends CI_Controller{
public function index(){
$data['main_view'] = 'login_form';
$this->load->view('login/include/template', $data);
}
public function validate(){
$this->load->library('form_validation');
$this->form_validation->set_rules('user', 'Име', 'trim|required|min_length[3]');
$this->form_validation->set_rules('pass', 'Парола', 'trim|required|min_length[4]');
if($this->form_validation->run()){
$this->load->model('users_model');
if($this->users_model->validate_login($username, $password)){
//valid user
}
else{
//not vlaid user
}
}
else{
$this->index();
}
}
}
and a model:
class Users_model extends CI_Model{
function __construct() {
parent::__construct();
}
function validate_login(){
$username = $this->input->post('user');
$password = md5($this->input->post('pass'));
$this->db->where('username', $username);
$this->db->where('password', $password);
$query = $this->db->get('users');
if($query->num_rows() == 1){
return TRUE;
}
else return FALSE;
}
}
When send via form (post) valid pair (username and password) nothing happens, but in apache2 log appears this:
PHP Fatal error: Call to a member function where() on a non-object in /var/www/ci/application/models/users_model.php
What is wrong?
Add a constructor to the Login class
class Login extends CI_Controller {
public function __construct() {
parent::__construct();
}
}
Do you have database loaded? You need to load the database first.
You can either add it to /config/autoload.php to autoload database function:
$autoload['libraries'] = array('database');
Or call on demand like this:
$this->load->database();
More details here
Do Not Use post in model instead use this in controller like this
controller login.php
public function validate(){
$this->load->library('form_validation');
$this->form_validation->set_rules('user', 'Име', 'trim|required|min_length[3]');
$this->form_validation->set_rules('pass', 'Парола', 'trim|required|min_length[4]');
if($this->form_validation->run()){
$this->load->model('users_model');
$post = $this->input->post();
$data['username'] = $post['username'];
$data['password'] = md5($post['password']);
if($this->users_model->validate_login($data)){
//valid user
}else{
//not vlaid user
}
}
else{
$this->index();
}
}
Model
function validate_login($where){
$this->db->where($where);
$query = $this->db->get('users');
if($query->num_rows() == 1){
return TRUE;
}
return FALSE;
}
This should work. Remember calling post somehow doesn't work in model. I dont exactly know they reason.

Codeigniter getinstance() error not loading any library,helpers?

i m creating a login system in codeigniter. i m new in codeigniter. i have getting this error when testing my login system
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: controllers/login.php
Line Number: 92
Fatal error: Call to a member function userdata() on a non-object in P:\xampp\htdocs\xxxxx\xxxxx\application\controllers\login.php on line 92
here is my login.php controller file
<?php
class Login extends CI_Controller {
function __construct()
{
if($this->is_logged_in() === true){
redirect('welcome');
}
}
function index()
{
$data['main_content'] = 'login_form';
$data['class_name'] = get_class();
$data['no_header'] = '1';
$data['no_footer'] = '1';
$this->load->view('includes/template', $data);
}
function validate_credentials()
{
$this->load->model('admin_model');
$query = $this->admin_model->validate();
print_r($query);
if($query) // if the user's credentials validated...
{
$data = array(
'AdminID' => $query,
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('welcome');
}
else // incorrect username or password
{
echo "here";
exit;
$this->index();
}
}
function signup()
{
$data['main_content'] = 'signup_form';
$this->load->view('includes/template', $data);
}
function create_member()
{
$this->load->library('form_validation');
// field name, error message, validation rules
$this->form_validation->set_rules('first_name', 'Name', 'trim|required');
$this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');
$this->form_validation->set_rules('email_address', 'Email Address', 'trim|required|valid_email');
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
$this->form_validation->set_rules('password2', 'Password Confirmation', 'trim|required|matches[password]');
if($this->form_validation->run() == FALSE)
{
$this->load->view('signup_form');
}
else
{
$this->load->model('membership_model');
if($query = $this->membership_model->create_member())
{
$data['main_content'] = 'signup_successful';
$this->load->view('includes/template', $data);
}
else
{
$this->load->view('signup_form');
}
}
}
function logout()
{
$this->session->sess_destroy();
$this->index();
}
function is_logged_in()
{
$CI =&get_instance();
$is_logged_in = $CI->session->userdata('is_logged_in');
if(isset($is_logged_in) || $is_logged_in === true)
{
return true;
}else{
return false;
}
}
}
i have tried
$CI =&get_instance();
$CI =get_instance();
$this->session->userdata
all three way but getting error.
you are calling the is_logged_in() function in the Controller's constructor without calling the parent constructor first. maybe that's probably why some resources aren't loaded yet.
try adding this:
function __construct()
{
parent::__construct(); //you always have to call parent's constructor before ANYTHING
if($this->is_logged_in() === true){
redirect('welcome');
}
}
You're not loading the session library for the new instance you create with get_instance().
Simply do, assuming session is already loaded and, as noted by #jere, you call the parent::__construct() in your controller:
function is_logged_in()
{
$is_logged_in = $this->session->userdata('is_logged_in');
if(isset($is_logged_in) || $is_logged_in === true)
{
return true;
}else{
return false;
}
}
Don't know why using another instance of the class here.
If you oughta do that, try loading the session library again, like:
$CI->load->library('session')
$is_logged_in = $CI->session->userdata('is_logged_in');
//rest of your old code

Fatal error: Call to a member function where() on a non-object in

I've been debugging this code but still not successful. Can anyone help me out please?
class Membership_model extends CI_Model {
function __construct()
{
parent::__construct();
}
function validate()
{
$this->db->where('username', $this->input->post('username'));
$this->db->where('password', md5($this->input->post('password')));
$query = $this->db->get('membership');
if($query->num_rows == 1)
{
return true;
}
}
function create_member()
{
$new_member_insert_data = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'email_address' => $this->input->post('email_address'),
'username' => $this->input->post('username'),
'password' => md5($this->input->post('password'))
);
$insert = $this->db->insert('membership', $new_member_insert_data);
return $insert;
}
}
I kept on getting an fatal error on the line
$this->db->where('username',$this->input->post('username'));
this is the controller/login.php
class Login extends CI_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
$this->load->helper('url');
$data['main_content'] = 'login_form';
$this->load->view('includes/template', $data);
}
function validate_credentials()
{
$this->load->model('membership_model');
$query = $this->membership_model->validate();
if($query) // if the user's credentials validated...
{
$data = array(
'username' => $this->input->post('username'),
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('site/members_area');
}
else // incorrect username or password
{
$this->index();
}
}
function signup()
{
$data['main_content'] = 'signup_form';
$this->load->view('includes/template', $data);
}
function create_member()
{
$this->load->library('form_validation');
// field name, error message, validation rules
$this->form_validation->set_rules('first_name', 'Name', 'trim|required');
$this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');
$this->form_validation->set_rules('email_address', 'Email Address', 'trim|required|valid_email');
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
$this->form_validation->set_rules('password2', 'Password Confirmation', 'trim|required|matches[password]');
if($this->form_validation->run() == FALSE)
{
$this->load->view('signup_form');
}
else
{
$this->load->model('membership_model');
if($query = $this->membership_model->create_member())
{
$data['main_content'] = 'signup_successful';
$this->load->view('includes/template', $data);
}
else
{
$this->load->view('signup_form');
}
}
}
function logout()
{
$this->session->sess_destroy();
$this->index();
}
The database probably is not being initialized properly before validate is called.
Looks like you're not connected to your DB. Make sure you are connecting to your database.
You can either connect automatically everytime your script runs or connect to the DB manually. Look at the CI guides for your connection options : http://codeigniter.com/user_guide/database/connecting.html
Could it be that the load->model( 'membership_model' ) should receive the name of a class, and that that name is case sensitive? You should probably check the return value of the API functions...

Categories