codeigniter 消息:ini_set():会话处于活动状态,此时不能更改会话模块的ini设置

tez616oj  于 2023-04-18  发布在  其他
关注(0)|答案(8)|浏览(274)

我用codeigniter创建了一个登录页面,但是我得到了php消息。
Message: ini_set(): A session is active. You cannot change the session module's ini settings at this time
如何解决这个问题?

view(login.php)

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Admin Login</title>
<link rel="stylesheet" href="../css/normalize.css">
<link rel="stylesheet" href="../css/admin-style.css">
</head>
<body>
    <?php echo form_open('Verify_login', ['id'=>'loginForm', 'name'=>'loginForm', 'method'=>'post']) ?>
    <div class="login">
        <div class="log-box">
            <h3>Login</h3>
            <div >
                  <input id="username" name="username" placeholder="User Name" value="" type="text" >
                <?php echo form_error('username'); ?>
                <input id="Password" name="password" placeholder="Password" required type="password">
                <?php echo form_error('password'); ?>
                <div class="remember-me">
                    <input id="checkBox" type="checkbox">
                    <label for="checkBox">Remember Me</label>
                </div>
                <button class="login-button" name="loginButton">Login</button>
            </div>
        </div>
    </div>
 </form>
</body>
</html>

控制器(Verify_login.php)

<?php 
defined('BASEPATH') OR exit('No direct script access aloowed');

class Verify_login extends CI_Controller
{

public function __construct()
{
    parent::__construct();
    $this->load->model('User');
    $this->load->helper('url');
    $this->load->helper('security');
    $this->load->library('form_validation');
    $this->load->library('session');
}

public function index()
{
    $this->form_validation->set_rules('username', 'Username', 'trim|required');
    $this->form_validation->set_rules('password', 'Password', 'trim|required|callback_check_database');
    if ($this->form_validation->run() == FALSE) {
        // if validation failed load the view 
        $this->load->view('admin/login');
    }

    else{
        $this->check_database($username , $password);
        redirect('dashboard', 'refresh');
    }
}

public function check_database($password)
{
    $username = $this->input->post('username');

    //query tha database
    $result = $this->User->login($username, $password);

    if ($result) {
        $sess_array = [];
        foreach ($result as $row) {
            $sess_array = 
                [
                    'id'=>$row->id,
                    'username'=>$row->name
                ];
            $this->session->set_userdata('logged_in', $sess_array);
        }
        return TRUE;
    }
    else{

        $this->form_validation->set_message('check_database','invalid username and password');
    }
  }
 }
?>

控制器(Admin.php)

session_start(); //need to call PHP's session object to access it though it
class Admin extends CI_Controller
{

public $data;
public function __construct()
{
    parent::__construct();
    $this->load->helper('url');

    $this->load->helper('form');
    $this->load->helper('url');
    $this->load->library('form_validation');
    $this->load->helper('security');

    //load user model
    $this->load->model('User');

}
public function index()
{
    // $this->load->view('admin/index');
    if ($this->session->userdata('logged_in')) {

        $session_data = $this->session->userdata('logged_in');
        $data['username'] = $session_data['name'];
        $this->load->view('admin/dashboard', $data);
    }
    else{

        //if no session redirect to login page 
        redirect('admin', 'refresh');
        // redirect('login');
    }
}

public function logout()
{
    $this->session->unset_userdata('logged_in');
    session_destroy();
    redirect('home', 'refresh');
}

model(User.php)

<?php

/***扩展MY_Model的用户登录类 * */

defined('BASEPATH') OR exit('no direct script allowed');

class User extends CI_Model
{
    protected $table = 'users';

    public function __construct()
    {
        $this->load->database();
    }

    public function login($username ,$password)
    {
        var_dump($username);
        var_dump($password);
        $this->db->select(['id', 'name', 'password']);
        $this->db->from($this->table);
        // $this->db->where('name', $username);
        // $this->db->where('password', $password);
        $this->db->limit(1);

        $query = $this->db->get();
        if ($query->num_rows() == 1) {
            return $query->result();
        }
        else{

            return false;
        }
    }
   }    
   ?>
ztmd8pv5

ztmd8pv51#

    • admin.php**中不需要这一行
session_start(); //need to call PHP's session object to access it though it

当您加载会话库时,它的构造函数会为您执行此操作。

cwdobuhd

cwdobuhd2#

该消息意味着您已经启动了一个与session_start()的会话,在代码中,您正在使用ini_set()操作会话模块。如果您正在操作会话模块,则应在会话启动和活动之前完成。

vxbzzdmp

vxbzzdmp3#

这对我真的很有用

if (!isset($_SESSION)) {

// server should keep session data for AT LEAST 24 hour
ini_set('session.gc_maxlifetime', 60 * 60 * 24);

// each client should remember their session id for EXACTLY 24 hour
session_set_cookie_params(60 * 60 * 24);
session_start();
}

因此,您确实在调用ini_set和session_set_cookie_params等之后启动会话

anhgbhbe

anhgbhbe4#

消息:ini_set():会话处于活动状态。此时无法更改会话模块的ini设置

我有相同的错误消息前一段时间,默认情况下代码点火器将写入文件(C:\xampp\tmp)在我的情况下.但我希望它写入数据库代替.
在你的自动加载器中,你可能有这样的东西:

//:::::$autoload['libraries'] = array();
//'session', this mean start session ini_set()
$autoload['libraries'] = array('session', 'database');

默认情况下,config/config.php会话中的配置看起来像这样:

$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = NULL;
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;

该文档指出,要使用数据库而不是文件,您应该首先在应用程序数据库中创建一个表。

CREATE TABLE IF NOT EXISTS `ci_sessions` (
        `id` varchar(128) NOT NULL,
        `ip_address` varchar(45) NOT NULL,
        `timestamp` int(10) unsigned DEFAULT 0 NOT NULL,
        `data` blob NOT NULL,
        KEY `ci_sessions_timestamp` (`timestamp`)
);

返回到 * config/config.php * 并将$config['sess_driver'] = 'files';更改为$config['sess_driver'] = 'database';
就像这样,我以为一切都应该很好,但我得到了错误消息:ini_set():会话处于活动状态,此时无法更改会话模块的ini设置
我的解决方案是首先运行session_destroy();
然后更改会话名和表名
我的配置看起来像这样:

$config['sess_driver'] = 'database';
$config['sess_cookie_name'] = 'newname_session';
$config['sess_expiration'] = 72000;
$config['sess_save_path'] ='newname_sessions';
$config['sess_match_ip'] = TRUE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;

保存所有内容,刷新页面后全部清空,没有错误信息

zz2j4svz

zz2j4svz5#

这对我很有效:

if (!isset($_SESSION)) {
   // server should keep session data for AT LEAST 24 hour
   ini_set('session.gc_maxlifetime', 60 * 60 * 24);

   session_start();

   // each client should remember their session id for EXACTLY 24 hour
   session_set_cookie_params(60 * 60 * 24);
}
mf98qq94

mf98qq946#

值得注意的是,在CI4中,如果你在模型中更改数据库,你会得到这个错误消息。如果你的会话不能更新,它会调用baseHandler fail(),这会导致输出错误。
解决方案是在Config中将App.php设置为public $sessionSavePath = 'db.table';,这样就可以修复它。

eqqqjvef

eqqqjvef7#

您可以通过仅在需要时运行命令来处理此问题,如下所示:

if (!isset($_SESSION)
    && !headers_sent()
) {
    session_start();
}
yc0p9oo0

yc0p9oo08#

1.转到file \system\libraries\Session\Session。
1.在第143行取消session_start()的注解,这样就完成了。

相关问题