2015-10-15 90 views
0

我不斷收到此錯誤忽略的session_start:笨 - 一個會議已經開始了 - ()

A PHP Error was encountered

Severity: Notice

Message: A session had already been started - ignoring session_start()

Filename: Session/Session.php

Line Number: 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; 
     } 
    } 
} 
?> 
+0

發表您的控制器以及 –

+0

編輯帖子包含控制器 – user3574766

回答

1

更改爲

$this->session->set_userdata('logged_in', $sess_array); 

$this->session->set_userdata($sess_array); 

添加笨會議

例0

方法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'); 
+0

我已經試過了,但對我沒有工作。 – Isuru

+0

沒有工作我是指? –

0

當你的php.ini文件有session.autostart開啓時發生此問題。你需要通過關閉這個功能:

  1. 編輯你的php.ini文件,並將其設置爲關閉 - 或聯繫您的網站託管服務提供商,爲您

  2. 做到這一點放置一個的.htaccess文件與下面的值你的web目錄的根目錄: 的php_flag session.auto_start 0

希望這會有所幫助。