我是Codeigniter的新手,我遇到了会话问题; 即使用户未成功登录,会话也会启动。 要么 用户成功登录但当我浏览站点时,会话再次开始丢弃用户信息并在我的数据库中启动新会话。
我完全失去了任何帮助,非常感谢。
site.php - 控制器
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Site extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('user_model');
}
public function index()
{
$this->load->library('session');
$this->load->helper('url');
$data['title']= "Welcome Learn Guitar Online";
$this->load->view('header',$data);
$this->load->view('home',$data);
$this->load->view('footer',$data);
}
public function form()
{
$this->load->library('session');
$this->load->helper('url');
$data['title']= "Login";
$this->load->view('header',$data);
$this->load->view('form',$data);
$this->load->view('footer',$data);
}
public function login()
{
$loginEmail=$this->input->post('loginEmail');
$loginPassword=$this->input->post('loginPassword');
$result=$this->user_model->login($loginEmail,$loginPassword);
if($result) $this->myaccount();
else $this->form();
}
}
user_model.php - 模型
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User_model extends CI_Model {
public function __construct()
{
parent::__construct();
}
function login($email,$password)
{
$password = sha1($password);
$this->db->where("Email",$email);
$this->db->where("Password",$password);
$query=$this->db->get("members");
if($query->num_rows()>0)
{
foreach($query->result() as $rows)
{
//add all data to session
$newdata = array(
'user_id' => $rows->Id,
'user_name' => $rows->First_Name,
'user_email' => $rows->Email,
'logged_in' => TRUE,
);
}
$this->session->set_userdata($newdata);
return true;
}
return false;
}
public function add_user()
{
$data=array(
'username'=>$this->input->post('user_name'),
'email'=>$this->input->post('email_address'),
'password'=>md5($this->input->post('password'))
);
$this->db->insert('user',$data);
}
}
?>
header.php - 在应用程序的每个页面上
<?php
$sid = $this->session->all_userdata();
if($sid) {
print_r($this->session->userdata); ;
} else {
session_start();
}
?>
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<title><?php echo $title; ?></title>
<meta name="description" content="">
<meta name="author" content="">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<link rel="stylesheet" type="text/css" href="<?php echo base_url(). "assets/css/style.css"; ?>"/>
<link rel="stylesheet" type="text/css" href="<?php echo base_url(). "assets/css/skeleton.css"; ?>" />
<link rel="stylesheet" type="text/css" href="<?php echo base_url(). "assets/css/layout.css"; ?>" />
<link rel="stylesheet" type="text/css" href="<?php echo base_url(). "assets/css/base.css"; ?>" />
<script language="JavaScript" type="text/javascript" src="<?php echo base_url(). "assets/js/jquery-1.2.6.min.js"; ?>"></script>
<script language="JavaScript" type="text/javascript" src="<?php echo base_url(). "assets/js/script.js"; ?>"></script>
<script language="JavaScript" type="text/javascript" src="<?php echo base_url(). "assets/js/jquery.tablesorter.pager.js"; ?>"></script>
<script language="JavaScript" type="text/javascript" src="<?php echo base_url(). "assets/js/jquery.tablesorter.min.js"; ?>"></script>
<script language="JavaScript" type="text/javascript" src="<?php echo base_url(). "assets/js/jquery.tablesorter.js"; ?>"></script>
<script language="JavaScript" type="text/javascript" src="<?php echo base_url(). "assets/js/comment.js"; ?>"></script>
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link rel="shortcut icon" href="images/favicon.ico">
<link rel="apple-touch-icon" href="images/apple-touch-icon.png">
<link rel="apple-touch-icon" sizes="72x72" href="images/apple-touch-icon-72x72.png">
<link rel="apple-touch-icon" sizes="114x114" href="images/apple-touch-icon-114x114.png">
</head>
答案 0 :(得分:0)
为什么使用session_start();在你看来。我无法看到这里有用。
要了解会话库,请使用此链接http://ellislab.com/codeigniter/user-guide/libraries/sessions.html
答案 1 :(得分:0)
load->library('session');
或将session class
设置为autoload
。
阅读CI文档,它会对您有所帮助。
感谢。
答案 2 :(得分:0)
要从会话中检索值以检查用户是否已登录,请使用此链接
public function verify_login(){
if($this->session->userdata('logged_in')!=TRUE){
redirect('site');
}
}
尝试在此{/ 1}}中调用此
construct
所以当页面加载时,它会在调用该控制器时检查第一个
而不是加载$this->verify_login();
&amp; library
在每个函数中,我建议您在配置文件夹中使用helper
您可以尝试将模型登录功能更改为此
autoload.php
和您的控制器登录功能
public function login(){
$this->db->where('Email',$this->input->post('loginEmail'));
$this->db->where('Password',sha1($this->input->post('loginPassword')));
$query=$this->db->get("members");
if($query->num_rows == 1){
foreach ($query->result() as $rows){
$newdata = array(
'user_id' => $rows->Id,
'user_name' => $rows->First_Name,
'user_email' => $rows->Email,
'logged_in' => TRUE
);
}
$this->session->set_userdata($newdata);
return true;
} else {
return false;
}
}
答案 3 :(得分:0)
如果您想检查用户是否已登录,我建议您使用pre_controller挂钩。
如果您感兴趣,可以使用一些身份验证插件:
http://haseydesign.com/flexi-auth/
http://konyukhov.com/soft/tank_auth/
如果codeigniter会话类仍然将会话数据直接存储在cookie中,这会让您感到不舒服,可以使用db session class