Reputation: 21
Hello I have Undefined property: stdClass::$email . Can you help me ?
A PHP Error was encountered
Severity: Notice
Message: Undefined property: stdClass::$email
Filename: controllers/verifylogin.php
Line Number: 48
Backtrace:
File: D:\wamp\www\codeigniter\application\controllers\verifylogin.php Line: 48 Function: _error_handler
File: D:\wamp\www\codeigniter\index.php Line: 292 Function: require_once
This is VerifyLogin Controller
class VerifyLogin extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('user_model','',TRUE);
}
function index()
{
//This method will have the credentials validation
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');
if($this->form_validation->run() == FALSE)
{
//Field validation failed. User redirected to login page
$this->load->view('login');
}
else
{
//Go to private area
// redirect('home', 'refresh');
}
}
public function check_database($password)
{
//Field validation succeeded. Validate against database
$email = $this->input->post('email');
//query the database
$result = $this->user_model->login($email, $password);
if($result)
{
$sess_array = array();
foreach($result as $row)
{
$sess_array = array(
'user_id' => $row->user_id,
'email' => $row->email
);
// $session_data = $this->session->set_userdata('logged_in',$sess_array);
$this->session->set_userdata('logged_in', $sess_array);
}
return TRUE;
}
else
{
$this->form_validation->set_message('check_database', 'Invalid email or password');
return false;
}
}
}
Home Controller
<?php
session_start(); //we need to call PHP's session object to access it through CI
class Home extends CI_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['email'] = $session_data['email'];
$this->load->view('home', $data);
}
else
{
//If no session, redirect to login page
redirect('login', 'refresh');
}
}
function logout()
{
$this->session->unset_userdata('logged_in');
session_destroy();
redirect('home', 'refresh');
}
}
User model
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class User_model extends CI_Model {
public function login($email, $password){
$this->db->select('user_id','email','password');
$this->db->from('users');
$this->db->where('email',$email);
$this->db->where('password',md5($password));
$this->db->limit(1);
$query = $this->db->get();
if($query->num_rows() == 1){
return $query->result();
}
else {
return false;
}
}
public function register($name, $surname, $email, $password)
{
$data = array(
'name' => $name,
'surname' => $surname,
'email' => $email,
'password' => md5($password)
);
if( ($name && $surname && $email && $password) != NULL){
$query = $this->db->insert('users', $data);
}
else{
return false;
}
}
}
Login form
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Logowanie</title>
</head>
<body>
<h1>Widok Logowania</h1>
<?php echo validation_errors(); ?>
<?php echo form_open('verifylogin'); ?>
<label for="email">Email:</label>
<input type="email" id="email" name="email">
<br>
<label for="password">Hasło: </label>
<input type="password" id="password" name="password">
<br>
<input type="submit" value="Zaloguj">
<?php echo form_close(); ?>
</body>
</html>
Upvotes: 1
Views: 4136
Reputation: 21
The Solution: user_model.php
This
$query = $this->db->select('user_id','email','password')
Change to
$query = $this->db->select('user_id,email','password')
MY GOD!
Upvotes: 1
Reputation: 22532
I think there is problem in your form_open() method
<?php echo form_open('verifylogin'); ?>
You get your variable in controller VerifyLogin
of function check_database()
So you form_open would be
<?php echo form_open('verifylogin/check_database'); ?>
and you forget to load
$this->load->helper('form');
try to read manual CI form helper
Upvotes: 1