【发布时间】:2015-10-15 15:30:04
【问题描述】:
我不断收到此错误:
遇到了 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;
}
}
}
?>
【问题讨论】:
-
也发布你的控制器
-
编辑帖子以包含控制器
标签: php codeigniter session