用户登录时其他页面上的 Codeigniter 3 会话数据
Codeigniter 3 Session Data On Other Pages While User Logged In
这里有这个问题(抱歉我很笨),最近刚开始使用 Codeigniter。
我的登录系统运行良好。我尝试在使用 index-header.php:
上的代码登录时转到主页
<?php
if( !isset($_SESSION) ){
echo '<a href="login" class="popupbox">Login</a>';
} else {
echo '<a href="dashboard/logout">Log Out</a>';
}
?>
并在 main_view.php(主页控制器)
public function __construct() {
parent::__construct();
$this->load->library('session');
}
public function index() {
if($this->session->userdata('is_logged_in')){
$data['title'] = "Home";
$this->load->view('headfoot/header-main',$data);
$this->load->view('main_view');
$this->load->view('headfoot/footer-main');
} else {
$data['title'] = "Home";
$this->load->view('headfoot/header-main',$data);
$this->load->view('main_view');
$this->load->view('headfoot/footer-main');
}
}
}
现在,如果我在登录状态下单击从主页注销,它会很好地断开会话,但不会在刷新后将主页中的文本更改回 "Login"。
换句话说,无论用户是否登录,它始终将文本显示为 "Logout"。
dashboard.php(控制器)
public function logout() {
$this->session->sess_destroy();
$data['title'] = "Logged out";
$data['logout_msg'] = "You have successfully logged out.";
$this->load->view('headfoot/header-login',$data);
$this->load->view('admin/login', $data);
$this->load->view('headfoot/footer-login');
}
创建 is_logged_in.php 一个单独的文件是个好习惯吗?如果是那么如何link会话呢?
使用$this->session->sess_destroy();
终止会话。检查 this URL。通过控制器终止会话然后使用 redirect
.
顺便说一下,在 application/core
文件夹下创建一个名为 MY_Controller.php
的文件,并在上面创建您的会话内容。如果你想了解更多,就google吧。
改变这个:
<?php
if( !isset($_SESSION) ){
至:
<?php if($this->session->userdata('username') == null ){
我在这里使用 'username'
假设您在允许用户登录时将用户名设置为会话数据。
这里有这个问题(抱歉我很笨),最近刚开始使用 Codeigniter。
我的登录系统运行良好。我尝试在使用 index-header.php:
上的代码登录时转到主页<?php
if( !isset($_SESSION) ){
echo '<a href="login" class="popupbox">Login</a>';
} else {
echo '<a href="dashboard/logout">Log Out</a>';
}
?>
并在 main_view.php(主页控制器)
public function __construct() {
parent::__construct();
$this->load->library('session');
}
public function index() {
if($this->session->userdata('is_logged_in')){
$data['title'] = "Home";
$this->load->view('headfoot/header-main',$data);
$this->load->view('main_view');
$this->load->view('headfoot/footer-main');
} else {
$data['title'] = "Home";
$this->load->view('headfoot/header-main',$data);
$this->load->view('main_view');
$this->load->view('headfoot/footer-main');
}
}
}
现在,如果我在登录状态下单击从主页注销,它会很好地断开会话,但不会在刷新后将主页中的文本更改回 "Login"。 换句话说,无论用户是否登录,它始终将文本显示为 "Logout"。
dashboard.php(控制器)
public function logout() {
$this->session->sess_destroy();
$data['title'] = "Logged out";
$data['logout_msg'] = "You have successfully logged out.";
$this->load->view('headfoot/header-login',$data);
$this->load->view('admin/login', $data);
$this->load->view('headfoot/footer-login');
}
创建 is_logged_in.php 一个单独的文件是个好习惯吗?如果是那么如何link会话呢?
使用$this->session->sess_destroy();
终止会话。检查 this URL。通过控制器终止会话然后使用 redirect
.
顺便说一下,在 application/core
文件夹下创建一个名为 MY_Controller.php
的文件,并在上面创建您的会话内容。如果你想了解更多,就google吧。
改变这个:
<?php
if( !isset($_SESSION) ){
至:
<?php if($this->session->userdata('username') == null ){
我在这里使用 'username'
假设您在允许用户登录时将用户名设置为会话数据。