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

时间:2015-09-28 02:23:29

标签: php codeigniter session apache2

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

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

如何解决这个问题?

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

型号(User.php)

<?php 

/ **     *用户登录claass,扩展了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;
        }
    }
   }    
   ?>

7 个答案:

答案 0 :(得分:9)

我认为你是框架概念和OOP的新手。

admin.php

中不需要此行
session_start(); //need to call PHP's session object to access it though it

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

答案 1 :(得分:4)

该消息表示您已启动与session_start()的会话,其中您正在使用ini_set()代码进一步向下操作会话模块。如果您正在操作会话模块,则应在会话启动并激活之前完成。

答案 2 :(得分:2)

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

我之前有过相同的错误消息,默认情况下,Code-igniter会在我的情况下写入文件( 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;

documentation声明要使用数据库而不是文件,首先应在应用程序数据库中创建一个表。

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;

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

答案 3 :(得分:1)

这真的对我有用

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等之后开始会话

答案 4 :(得分:0)

  1. 转到文件\ system \ libraries \ Session \ Session。
  2. 在第143行取消注释session_start(),您就完成了。

答案 5 :(得分:0)

这对我有用:

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

答案 6 :(得分:-1)

您可以通过仅在需要时运行命令来解决此问题:

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