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
Related
I'm working with sessions for a login function. However every time I attempt log in it just keeps redirecting to the login screen. Below is the code. There is a function set to say that the login is invalid, that is not sending out an error so I'm not sure that it is a login issue.
ATIS Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Atis extends CI_Controller
{
public $status;
public $roles;
function __construct()
{
parent::__construct();
$this->load->model('User_model', 'user_model', TRUE);
$this->status = $this->config->item('status');
$this->roles = $this->config->item('roles');
$this->is_logged_in();
}
function index(){
$this->load->view("atis/inc/header");
$this->load->view("atis/dashboard");
$this->load->view("atis/inc/footer");
}
function is_logged_in()
{
$is_logged_in = $this->session->userdata('is_logged_in');
if(!isset($is_logged_in) || is_logged_in !==true)
{
redirect('main/login');
}
}
Main Controller
public function login()
{
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'required');
if($this->form_validation->run() == FALSE) {
$this->load->view('inc/site_header');
$this->load->view("site_nav");
$this->load->view('login');
$this->load->view('inc/site_footer');
}else{
$post = $this->input->post();
$clean = $this->security->xss_clean($post);
$userInfo = $this->user_model->checkLogin($clean);
if(!$userInfo){
$this->session->set_flashdata('flash_message', 'The login was unsucessful');
redirect(site_url().'main/login');
}
foreach($userInfo as $key=>$val){
$this->session->set_userdata($key, $val);
$this->session->set_userdata('is_logged_in');
}
redirect(site_url().'atis/');
}
}
Check login method
public function checkLogin($post)
{
$this->load->library('password');
$this->db->select('*');
$this->db->where('email', $post['email']);
$query = $this->db->get('users');
$userInfo = $query->row();
if(!$this->password->validate_password($post['password'], $userInfo->password)){
error_log('Unsuccessful login attempt('.$post['email'].')');
return false;
}
$this->updateLoginTime($userInfo->id);
unset($userInfo->password);
return $userInfo;
}
Yard Function from ATIS Controller
public function yard()
{
$this->load->model('atisyard_model');
$this->data['yards'] = $this->atisyard_model->getyardname();
$this->data['trackdatabase'] = $this->atisyard_model->gettrains();
$this->load->view("atis/inc/header");
$this->load->view('atis/yard_view', $this->data);
$this->load->view("atis/inc/footer");
}
You are missing a $ in if(!isset($is_logged_in) || is_logged_in !==true)
Or if still does not work, try changing
if(!isset($is_logged_in) || is_logged_in !==true)
to
if(!$is_logged_in)
$this->session->set_userdata('is_logged_in');
is not correct. You should pass something like
$this->session->set_userdata('is_logged_in', TRUE);
or
$this->session->set_userdata($arr);
to have it valid. Docs.
Edit:
In controller, change this
if(!$userInfo)
{
$this->session->set_flashdata('flash_message', 'The login was unsucessful');
redirect(site_url().'main/login');
}
foreach($userInfo as $key=>$val)
{
$this->session->set_userdata($key, $val);
$this->session->set_userdata('is_logged_in');
}
redirect(site_url().'atis/');
to this
if(!$userInfo)
{
$this->session->set_flashdata('flash_message', 'The login was unsucessful');
redirect(site_url().'main/login');
}
else
{
foreach($userInfo as $key=>$val)
{
$this->session->set_userdata($key, $val);
}
$this->session->set_userdata('is_logged_in', TRUE);
redirect(site_url().'atis/');
}
Edit: 20160130002859
In is_logged_in() method you are checking if(!isset($is_logged_in) || $is_logged_in !==true) which is paradoxal. In any case right side of condition will be FALSE. This is because of fact $is_logged_in nevere can be boolean TRUE by type if not casted explicitly. It is object. So you should check in other ways:
$is_logged_in != TRUE // this way you don't check for type
or
is_object($is_logged_in)
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
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');
}
I have placed 2 controller login and site1.When i login and enter to members area to view pages and if i copy that url and placed in another tab.it opens.But my aim is to display u have no permission to access the page.
Controller:login
<?php
class Login extends CI_Controller {
function index()
{
$data['main_content'] = 'login_form';
$this->load->view('includes/template', $data);
}
function inactive()
{
echo"<script>alert('In active user Please contact the administrator');</script>";
$this->load->view('login_form');
}
function invalid()
{
echo"<script>alert('Invalid username or password');</script>";
$this->load->view('login_form');
}
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
);
if($query->num_rows()>0){
$status = $query->row()->account_status;}
else {
$status = ''; }
//Account active
if($status == 'active')
{
$this->session->set_userdata($data);
redirect('site1/members_area');
}
else if ($status == 'inactive')//Account In active
{ $this->inactive();
}
else // incorrect username or password
{
$this->invalid();
}
}
}
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('college_name', 'college_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]');
$this->load->helper('date');
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();
}
site1:
<?php
class Site1 extends CI_Controller
{
function members_area()
{
$this->load->view('index');
}
function another_page() // just for sample
{
echo 'good. you\'re logged in.';
}
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');
}
}
}
}
Sessions in PHP are carried over between tabs. The server doesn't really know how to seperate between two tabs. Read the PHP docs on sessions for more information http://us2.php.net/manual/en/intro.session.php
I have implemented a basic login system using CodeIgniter. I am using the sessions library to control access to a members only section. I can log in and view this area no problem. However when I delete my cookies and refresh the members only section page I can still see the content. I am not displaying a login message to the user.
I don't know why this is happening. Any ideas?
This is my Site.php controller
class Site extends CI_Controller{
function _construct(){
parent::_construct();
$this->is_logged_in();
}
function members_area(){
$this->load->view('members_area');
}
function is_logged_in(){
$is_logged_in = $this->session->userdata('is_logged_in');
if(!isset($is_logged_in) || $is_logged_in != true){
echo 'You need to login to access this page. Login';
die();
}
}
}
This is my login.php Controller
class Login extends CI_Controller{
function index(){
$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 credentials validated
$data = array(
'username' => $this->input->post('username'),
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('site/members_area');
}
else{//If not validated re load login form
$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->signup();
}
else{
//create a new row in db
$this->load->model('membership_model');
if($query = $this->membership_model->create_member()){
//Info entered
$data['main_content'] = 'signup_successful';
$this->load->view('includes/template', $data);
}
else{
$this->load->view('signup_form');
}
}
}
}
This is the code that should be executed if a session does not exist. It is found in the site.php file.
if(!isset($is_logged_in) || $is_logged_in != true){
echo 'You need to login to access this page. Login';
die();
Any help is much appreciated!
Looks like your constructor isn't being called.
Perhaps it is because it needs two underscores, not 1 :)
function __construct(){
parent::__construct();
$this->is_logged_in();
}