我一直收到这个错误:
遇到PHP错误
严重性:注意
消息:会话已经启动 - 忽略session_start()
文件名:Session / Session.php
行号:140
在包含此代码的页面上:
<body>
<p><a href="<?php echo site_url("Login_controller")?>">LOGIN</a><p>
<?php echo $this->session->userdata('Username'); ?>
</body>
页面上没有session_start()。
我能想到的唯一一件事就是在autoload.php中我做了这个:
$autoload['libraries'] = array('database','session');
导致此错误的原因是什么?
编辑:登录控制器
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login_controller extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('User_model','',TRUE);
}
function index() //Default function that is run when this controller is called.//
{
$this->load->helper(array('form'));
$this->load->view('Login_view');
}
function Login()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('Username', 'Username', 'trim|required');
$this->form_validation->set_rules('Password', 'Password', 'trim|required|callback_CheckDatabase');
if($this->form_validation->run() == FALSE)
{
$this->load->view('Login_view'); //Reloads the login view with validation errors if the login attempt is unsuccessful.//
}
else
{
redirect('Home_controller'); //Redirect to the homepage on successful login attempt.//
}
}
function CheckDatabase($password) //This function is only run when password validation is correct.//
{
$username = $this->input->post('Username'); //Sets the username as a $username.//
$result = $this->User_model->Login($username, $password);
if($result)
{
$sess_array = array();
foreach($result as $row)
{
$sess_array = array( //Makes an array of the data to be stored in the session.//
'UserID' => $row->UserID,
'Username' => $row->Username
);
$this->session->set_userdata('logged_in', $sess_array); //Sets $sess_array as the session.//
}
return TRUE;
}
else //Ran if the username or password aren't matched in the CIUsers database. Returns error message.//
{
$this->form_validation->set_message('CheckDatabase', 'Invalid login details.');
return false;
}
}
}
?>
将此更改为
$this->session->set_userdata('logged_in', $sess_array);
这个
$this->session->set_userdata($sess_array);
方法01
$newdata = array(
'username' => 'johndoe',
'email' => '[email protected]',
'logged_in' => TRUE
);
//$newdata = array(
// 'UserID' => $row->UserID,
// 'Username' => $row->Username,
// 'logged_in' => TRUE
// );
$this->session->set_userdata($newdata);
方法02
$this->session->set_userdata('some_name', 'some_value');
// $this->session->set_userdata('my_name', 'user3574766');
当您的PHP.INI文件打开session.autostart时会发生此问题。您需要通过以下方式关闭此功能:
希望这可以帮助。