Codeigniter Session Error:

Asked

Viewed 428 times

2

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

ini_set('session.use_trans_sid', 0);

ini_set('session.use_cookies', 1);

session_start();

Erro de Sessão

Standard Controller

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

USERNAME:
Password:

Class Model 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();                         
            }         
        }

}

Verify 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');
 }

}

1 answer

4


Though it’s 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 stunned, you call another session_start() in the Home class, maybe that’s the problem. In the codeigniter you don’t need to use session_start(), just load the library Session into the autoloader. Next:

Access the file: application/config/autoload.php

Look for the Libraries variable and add the library Session as the example:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.