0
votes

I keep getting this error:

A PHP Error was encountered

Severity: Notice

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

Filename: Session/Session.php

Line Number: 140

On a page with this code:

<body>
<p><a href="<?php echo site_url("Login_controller")?>">LOGIN</a><p> 
<?php echo $this->session->userdata('Username'); ?>
</body>

There is no session_start() on the page.

The only thing I can think has caused this is in autoload.php i have done this:

$autoload['libraries'] = array('database','session');

What is causing this error?

Edit: Login controller

    <?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;
        }
    }
}
?>
2
post your controller as well - Abdulla Nilam
Edited the post to contain controller - user3574766

2 Answers

1
votes

Change this to

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

this

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

Example of Adding Codeigniter Session

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

Method 02

$this->session->set_userdata('some_name', 'some_value');
// $this->session->set_userdata('my_name', 'user3574766');
0
votes

This problem occurs when your PHP.INI file has session.autostart turned on. You'll need to turn this off by:

  1. Editing your PHP.INI file and setting it to off -- or contacting your web hosting provider to do this for you

  2. Placing a .htaccess file in the root of your web directory with the following value: php_flag session.auto_start 0

hope this helps.