I was trying to do a login in CodeIgniter and I got this error in my model and admin_home.php. I have tried to fix it, but still haven't got an answer. Please do help if you can.
ERROR
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Admin_login::$session
Filename: core/Model.php
Line Number: 52
Fatal error: Call to a member function set_userdata() on a non-object in /.../admin_login_model.php on line 31
This is my code.
admin_login.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Admin_login extends CI_Controller {
function __construct()
{
parent::__construct();
/* Load the model*/
$this->load->model('admin_login_model');
}
public function index($msg = NULL){
// Load our view to be displayed
// to the user
$data['msg'] = $msg;
$this->load->view('admin_login_view', $data);
}
public function process(){
// Validate the user can login
$result = $this->admin_login_model->validate();
// Now we verify the result
if(! $result){
// If user did not validate, then show them login page again
$msg = '<font color=red>Invalid username and/or password.</font><br />';
$this->index($msg);
}else{
// If user did validate,
// Send them to members area
redirect('admin_home');
}
}
}
?>
admin_login_view.php
<!doctype html>
<html>
<head>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"
rel="stylesheet">
<link href='https://fonts.googleapis.com/css?family=Libre+Baskerville' rel='stylesheet' type='text/css'>
<style>
body {
padding-top: 70px; /* needed to position navbar properly */
font-family: 'Libre Baskerville', serif;
}
</style>
<title>Admin Login!</title>
</head>
<body>
<nav class="navbar navbar-default navbar-fixed-top navbar-inverse">
<div class="container">
<div class="container-fluid">
<div class="navbar-header">
<p class="navbar-brand">
<a href="">Admin Login!</a>
</p>
</div>
</div>
</div>
</nav>
<div class="container" id="login_form">
<form action='https://w1439345.users.ecs.westminster.ac.uk/ECWM604/ci/index.php/admin_login/process' method='post' name='process'>
<h2>Login</h2>
<br />
<?php if(! is_null($msg)) echo $msg;?>
<label for='username'>Username</label>
<input type='text' name='username' id='username' size='25' /><br />
<label for='password'>Password</label>
<input type='password' name='password' id='password' size='25' /><br />
<input type='Submit' value='Login' />
</form>
</div> <!-- container -->
</body>
</html>
admin_login_model.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Admin_login_model extends CI_Model {
function __construct()
{
parent::__construct();
$this->load->database();
}
public function validate(){
/* grab admin input*/
$username = $this->security->xss_clean($this->input->post('username'));
$password = $this->security->xss_clean($this->input->post('password'));
/* Prep the query*/
$this->db->where('username', $username);
$this->db->where('password', $password);
/* Run the query*/
$query = $this->db->get('admin');
/* check if there are any results*/
if($query->num_rows() == 1)
{
// If there is a user,create session data
$row = $query->row();
$data = array(
'username' => $row->username,
'validated' => true
);
$this->session->set_userdata($data);
return true;
}
/* If the previous process did not validate
then return false.*/
return false;
}
}
?>
and admin_home.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Admin_home extends CI_Controller{
function __construct(){
parent::__construct();
$this->check_isvalidated();
}
public function index(){
/* If the admin is validated, then this function will run*/
echo 'Congratulations, you are logged in.';
}
private function check_isvalidated(){
if(! $this->session->userdata('validated')){
redirect('admin_login');
}
}
public function do_logout(){
$this->session->sess_destroy();
redirect('admin_login');
}
}
?>
Thank you for your help.