iam试图创建一个简单的登录会话,但我被重定向到登录页面,这是我的Login.php控制器。
> <?php class Login extends CI_Controller
{ function __construct()
{
> parent::__construct();
if($this->session->userdata('admin'))
> redirect('admin/dashboard');
}
function index()
{
> $this->load->view('admin/login', $data);
} function verify() {
> //username:admin password:123456
$this->load->model('admin');
> $check = $this->admin->validate();
if($check) {
> $this->session->set_userdata('admin','1');
> redirect('admin/dashboard');
}
else
{
>
redirect('admin'); } }
>
>
>
> }
这是我的Dashboard.php控制器,如果用户名和密码正确,ademin会重新使用它
<?php
class Dashboard extends CI_Controller
{
function __construct()
{
parent::__construct();
if(!$this->session->userdata('admin'))
redirect('admin');
}
function index()
{
$this->load->view('admin/dashboard');
}
function logout()
{
$this->session->sess_destroy();
redirect('admin');
}
}
这里是database.php
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'ASGB-test',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
)
;
这是我的autoload.php,我在这里激活数据库和会话库
$autoload['libraries'] = array('database','session');
这里是routes.php,欢迎控制器基本上是原始的codeigniter欢迎视图,我也定义了管理路由
$route['default_controller'] = 'welcome';
$route['admin'] = 'admin/login';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
这里是Admin_model.php
<?php
class Admin_model extends CI_Model
{
function validate()
{
$arr['username'] = $this->input->post('Username');
$arr['password'] = md5($this->input->post('Password'));
return $this->db->get_where('admins',$arr)->row();
}
}
我确信我输入了正确的密码和用户名,但它只是将我重定向到登录页面
您必须使用
session_start();
在需要会话变量的每个页面的php脚本的最开始。
解决了这个问题,我实际上拼写错误了"用户名";以及";密码";,通过解决这个问题,我成功登录。谢谢你的回答
首先,我不知道$data在登录控制器的index((中做什么,也不知道validate((在您的模型管理中将做什么,然后我认为最好将登录和注销功能放在登录控制器中,如下所示:
<?php
class Login extends CI_Controller{
function __construct(){
parent::__construct();
}
function index() {
$this->load->view('admin/login');
}
public function log_in()
{
$this->load->model('admin');
$this->form_validation->set_rules('username', 'Username', 'trim|required', array('required' => 'Username required'));
$this->form_validation->set_rules('password', 'Password', 'trim|required', array('required' => 'Password required'));
//username:admin password:123456
if ($this->form_validation->run() !== false) {
$username = $this->input->post('username');
$password = $this->input->post('password');
$check = $this->admin->validate($username, $password);
if ($check != false) {
$this->session->set_userdata('admin','1');
redirect('Dashboard');
} else {
redirect('Login');
}
}
$this->load->view('admin/login');
}
public function log_out()
{
$this->session->sess_destroy();
redirect('Login');
}
}
?>
然后您的Dasboard控制器如下:
<?php
class Dashboard extends CI_Controller
{
function __construct()
{
parent::__construct();
if(!$this->session->userdata('admin'))
redirect('Login');
}
function index()
{
$this->load->view('admin/dashboard');
}
}
?>
以及Admin模型,如下所示:
<?php
class Admin extends CI_Model
{
function validate($username, $password)
{
$this->db->select('*')
->from('admins')
->where('username', $username)
->where('password', md5($password));
$query = $this->db->get();
return $query->result();
}
}
?>