我正在尝试在codeigniter中创建会话并照常执行步骤但是在显示会话数据的同时我收到错误未定义的变量电子邮件我试图找出错误但没有成功
// controller Logincontroller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
print_r( debug_backtrace() );
//ini_set('memory_limit', '-1');
//ini_set('max_execution_time', 3000);
ini_set('display_errors',1);
error_reporting(E_ALL);
class LoginController extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library('session');
$this->load->helper('form');
$this->load->helper('url');
$this->load->helper('html');
$this->load->database();
$this->load->library('form_validation');
}
public function index()
{
//load the login model
$this->load->model('login_model');
// $qry=$this->login_model->validate();
//get the posted values
$username = $this->input->post("username");
$password = $this->input->post("password");
//set validations
$this->form_validation->set_rules("username", "username", "trim|required");
$this->form_validation->set_rules("password", "password", "trim|required");
if ($this->form_validation->run() == FALSE)
{
//validation fails
redirect('Homecontroller/index'); //make new controller for loading a form again
echo "Validation fails"; // even for no name and passwrd
}
else
{
//validation succeeds
if ($this->input->post('submit') == "Login")
{
//check if username and password is correct
$usr_result = $this->login_model->get_user($username, $password);
if ($usr_result > 0) //active user record is present
{
//set the session variables
// $sessiondata = array(
// 'username' => $username,
//'loginuser' => TRUE
//);
$this->login_model->set_session($username,$email);//line 58
print_r($this->session->all_userdata()); //to check
return $username;
redirect("Site/member_area");
}
else
{
$this->session->set_flashdata('msg', '<div class="alert alert-danger text-center">Invalid username and password!</div>');
redirect('Homecontroller/index');
}
}
else
{
redirect('login/home3');
}
}
}
}?>
//这是我的模型login_model
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login_model extends CI_Model
{
function __construct()
{
// Call the Model constructor
parent::__construct();
}
//get the username & password from tbl_usrs
public function get_user($usr, $pwd)
{
$sql = "select * from tbl_usrs where username = '" . $usr . "' and password = '" .$pwd . "' ";
$query = $this->db->query($sql);
return $query->num_rows();
}
public function set_session($username,$email) {
$sql="SELECT id FROM tbl_usrs WHERE username='".$username."' LIMIT 1 ";
$result=$this->db->query($sql);
$row=$result->row();
$sess_data=array (
'id'=>$row->id,
'username'=>$username,
'email'=>$email,//line 30
'is_login'=>TRUE
);
$this->session->set_userdata($sess_data);
} //set_seesion function ends
}?>
这就是我得到的
Array ( [__ci_last_regenerate] => 1438028278 [id] => 4 [username] => aman [email] => [is_login] => 1 )
答案 0 :(得分:1)
我对codeigniter不是很熟悉,但我觉得问题就在这里
public function set_session($username,$email) {
$sql="SELECT id FROM tbl_usrs WHERE username='".$username."' LIMIT 1 ";
$result=$this->db->query($sql);
$row=$result->row();
$sess_data=array (
'id'=>$row->id,
'username'=>$username,
'email'=>$email,
'is_login'=>TRUE
);
$this->session->set_userdata($sess_data);
在上面的代码中,您只是从数据库中提取id
,因此我认为它应该如下所示,同时获取id
和email
public function set_session($username,$email) {
$sql="SELECT id, email FROM tbl_usrs WHERE username='".$username."' LIMIT 1 ";
$result=$this->db->query($sql);
$row=$result->row();
$sess_data=array (
'id'=>$row->id,
'username'=>$username,
'email'=>$row->email,
'is_login'=>TRUE
);
$this->session->set_userdata($sess_data);