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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32814895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:13:04  来源:igfitidea点击:

Message: ini_set(): A session is active. You cannot change the session module's ini settings at this time

phpcodeignitersessionapache2

提问by noushid p

I created a login page with codeigniter,but i get the php message.

我用 codeigniter 创建了一个登录页面,但我收到了 php 消息。

Message: ini_set(): A session is active. You cannot change the session module's ini settings at this time

Message: ini_set(): A session is active. You cannot change the session module's ini settings at this time

how to fix this?

如何解决这个问题?

view (login.php)

查看(登录.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>

controller (Verify_login.php)

控制器(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');
    }
  }
 }
?>

controller(Admin.php)

控制器(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)

模型(User.php)

<?php 

/** *user login claass which extends MY_Model * */

/** *扩展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;
        }
    }
   }    
   ?>

回答by Sanoob

You don't need this line in admin.php

您不需要admin.php 中的这一行

session_start(); //need to call PHP's session object to access it though it

When you load session library it's constructor does these for you.

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

回答by Jesse

The message means that you have started a session with session_start()in which further down in the code you are using ini_set()to manipulate the session module. If you are manipulating the session module, it should be done before a session is started and active.

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

回答by ShapCyber

Message: ini_set(): A session is active. You cannot change the session module's ini settings at this time

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

I had same error message sometime ago , By default Code-igniter will write to file (C:\xampp\tmp) in my case. But I want it to write to database instead.

我前一段时间有同样的错误消息,默认情况下,Code-igniter 将写入文件(C:\xampp\tmp)在我的情况下。但我希望它改为写入数据库。

In your autoloader you might have had something like this:

在您的自动加载器中,您可能遇到过这样的事情:

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

By default setting in the config/config.php session configurartion look like this:

默认情况下 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;

The documentationstated that to use database instead of file you should first create a table in application database.

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

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`)
);

Go back to the config/config.phpand change $config['sess_driver'] = 'files';to $config['sess_driver'] = 'database';

回到config/config.php并更改$config['sess_driver'] = 'files';$config['sess_driver'] = 'database';

Like so I thought everything should be fine but I got the error Message: ini_set(): A session is active. You cannot change the session module's ini settings at this time

就像这样,我认为一切都应该没问题,但我收到了错误消息:ini_set(): A session is active。您此时无法更改会话模块的 ini 设置

My Solution was to first run the session_destroy();

我的解决方案是首先运行 session_destroy();

Then change session name and table name

然后更改会话名称和表名称

My config look like this :

我的配置是这样的:

$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;

Save everything and after refresh the page all cleared, no error message showing

保存所有内容,刷新页面后全部清除,没有显示错误消息

回答by I B

This is really working for me

这真的对我有用

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();
}

so you do start your session after calling ini_set and session_set_cookie_params etc

所以你确实在调用 ini_set 和 session_set_cookie_params 等之后开始你的会话

回答by cjohansson

You can handle this issue by only running the command when it's needed like this:

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

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

回答by priya soran

  1. Go to file \system\libraries\Session\Session.
  2. Uncomment session_start() at line 143 and you are done.
  1. 转到文件 \system\libraries\Session\Session。
  2. 取消第 143 行的 session_start() 注释,您就完成了。