[英]login session is not working codeigniter 3.1.11 , php 7.4
iam trying to make a simple login session, but i get redirected to login page, here is my Login.php controller.我正在尝试进行简单的登录 session,但我被重定向到登录页面,这是我的 Login.php controller。
> <?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'); } }
>
>
>
> }
<?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');
}
}
and here is database.php这是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
)
;
here is my autoload.php, i activate database and session libraries here这是我的 autoload.php,我在这里激活数据库和 session 库
$autoload['libraries'] = array('database','session');
here is routes.php, welcome controller is basically the original codeigniter welcome view, and i define the admin rout as well这里是 routes.php,欢迎 controller 基本上是原始的 codeigniter 欢迎视图,我也定义了管理员路由
$route['default_controller'] = 'welcome';
$route['admin'] = 'admin/login';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
here is Admin_model.php这里是 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();
}
}
I am sure im entering correct password and username, but it just redirect me to the login page我确定我输入了正确的密码和用户名,但它只是将我重定向到登录页面
you have to use你必须使用
session_start();
at the very beginning of the php script of every page that needs session variables.在需要 session 变量的每个页面的 php 脚本的开头。
First, I don't know what the $data is doing in your index() of the Login controller, and what the validate() is doing in your model admin , then I think it would be better to put the login and logout function in the Login Controller as the following:首先,我不知道$data在您的登录 controller 的index()中做了什么,以及validate()在您的model admin中做了什么,那么我认为最好将登录和注销 ZC394C4252648E16在登录 Controller 如下:
<?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');
}
}
?>
then your Dasboard Controller as follows:然后你的Dasboard Controller如下:
<?php
class Dashboard extends CI_Controller
{
function __construct()
{
parent::__construct();
if(!$this->session->userdata('admin'))
redirect('Login');
}
function index()
{
$this->load->view('admin/dashboard');
}
}
?>
and the Admin model as follows:和管理员model 如下:
<?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();
}
}
?>
Solved, i actually was miss spelling the "username" and "password", by solving that i logged in successfully.thanks for your answers解决了,我实际上是错过了拼写“用户名”和“密码”,通过解决我成功登录。谢谢你的回答
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.