Codeigniter Session Error:

2

This system error appears in codeigniter. The errors pointed out by php:

ini_set('session.use_trans_sid', 0);

ini_set('session.use_cookies', 1);

session_start();

Standard Driver      

class Login extends CI_Controller {   

        function __construct(){
          parent::__construct();
        }

    public function index(){    
            $this->load->helper(array('form')); 
            $this->load->view('login2');      
    }
}
?>

Login View                    Simple Login with CodeIgniter                    

Simple Login with CodeIgniter

                         USER NAME:                    Home          Password:                    

Class Template     class LoginModel extends CI_Model {

    # VALIDA USUÁRIO             
        function login($loginUsuario, $senhaUsuario){
            $this -> db -> select('loginUsuario, senhaUsuario, nomeUsuario');
            $this -> db -> from('usuarios');
            $this -> db -> where('loginUsuario', $loginUsuario);
            //$this -> db -> where('password', MD5($password));
            $this -> db -> where('senhaUsuario',$senhaUsuario);
            $this -> db -> limit(1);

            $query = $this -> db -> get();

            if($query -> num_rows() == 1){
              return $query->result();
            }
            else{
              return false;
            }
        }


        # VERIFICA SE O USUÁRIO ESTÁ LOGADO     
        function logged() {         
            $logged = $this->session->userdata('logged');         
            if (!isset($logged) || $logged != true) {             
                echo 'Voce nao tem permissao para entrar nessa pagina. Efetuar Login';             
                die();                         
            }         
        }

}

Check Login

**

<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Description: Classe para Verificar login do sistema
 * @author Alex José Silva
 * @package login
 */
class verificarLogin extends CI_Controller {

 function __construct(){
   parent::__construct();
   $this->load->model('LoginModel','',TRUE);
 }

 function index(){
   //This method will have the credentials validation
   $this->load->library('form_validation');

   $this->form_validation->set_rules('loginUsuario', 'loginUsuario', 'trim|required');
   $this->form_validation->set_rules('senhaUsuario', 'senhaUsuario', 'trim|required|callback_check_database');

   if($this->form_validation->run() == FALSE){
     //Field validation failed.  User redirected to login page
     $this->load->view('login2');
   }
   else{
     //Go to private area
     redirect('home', 'refresh');
   }

 }

 function check_database($senhaUsuario){
   //Field validation succeeded.  Validate against database
   $loginUsuario = $this->input->post('loginUsuario');

   //query the database
   $result = $this->LoginModel->login($loginUsuario, $senhaUsuario);

   if($result)
   {
     $sess_array = array();
     foreach($result as $row)
     {
       $sess_array = array(
         'nomeUsuario' => $row->nomeUsuario
       );
       $this->session->set_userdata('logged_in', $sess_array);
     }
     return TRUE;
   }
   else
   {
     $this->form_validation->set_message('check_database', 'Invalid username or password');
     return false;
   }
 }
}
?>

**

 <?php 
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description: Classe home apos lógin
 *
 * @author
 */

session_start(); //we need to call PHP's session object to access it through CI
class Home extends CI_Controller {



 function index()
 {
   if($this->session->userdata('logged_in')){
     $session_data = $this->session->userdata('logged_in');
     $data['loginUsuario'] = $session_data['loginUsuario'];
     $this->load->view('home', $data);
   }
   else{
     //If no session, redirect to login page
     redirect('login', 'refresh');
   }

   //corpo da pagia
    $this->load->view('elementos/cabecalho',$dados);
    $this->load->view('elementos/menuSuperior',$dados);
    $this->load->view('elementos/menuInferior',$dados);
    $this->load->view('elementos/conteudo',$dados);
    $this->load->view('elementos/rodape',$dados);
 }


 //saida do sistema
 function logout()
 {
   $this->session->unset_userdata('logged_in');
   session_destroy();
   redirect('home', 'refresh');
 }

}
    
asked by anonymous 16.05.2016 / 19:05

1 answer

3

Although it is not clear where you call ini_set('session.use_trans_sid', 0); ini_set('session.use_cookies', 1); session_start(); , the error says that a session is already active and exited, you call another session_start () in the Home class, maybe the problem there. In codeigniter you do not need to use session_start (), just load the library session in autoloader. Following:

Access the file: application / config / autoload.php

Search for the libraries variable and add the library session as the example:

$autoload['libraries'] = array('session');
    
17.05.2016 / 17:18