-5

I'm having an error trying to login, these two errors appear

A PHP Error was encountered Severity: Warning

Message: Illegal string offset 'titulo'

Filename: controllers/Login.php

second error

A PHP Error was encountered Severity: Notice

Message: Undefined variable: body

Filename: templates/login_template.php

follow the codes in order

<?php
if (! defined('BASEPATH')) exit('No direct script access allowed'); // linha de proteção ao controller
class Login extends CI_Controller {

    public function __construct() {
        parent::__construct();  
        $this->load->model('ControleAcessoModel', 'CA');    
        //$this->output->enable_profiler(true);
    }
    public function index($dados=""){       

        $dados['titulo'] = 'Autenticação';
        $this->template->load('login_template', 'area_restrita/acesso_login.php', $dados);
        //print_r($dados);
    }
    public function logout(){
        $this->CA->logout();    
    }
    public function autenticar(){

        $form = $this->input->post(NULL, TRUE);

        $this->load->library('form_validation');

        //validação dos campos
        $this->form_validation->set_rules('senha','Senha','trim|required',
            array(
                'required' => 'Informe uma valor para %s.'
            )
        );
        $this->form_validation->set_rules('login','Login','trim|required',
            array(
                'required' => 'Informe uma valor para %s.'
            )
        );

        $this->form_validation->set_error_delimiters('<div class="alert alert-danger">','</div>');

        if($this->form_validation->run() == true){
            $login = $this->input->post("login");
            $senha = $this->input->post("senha"); 

            if(!$this->CA->logged()){               
                $this->load->model('PessoaSistemaModel','objAutenticavel');
                $this->objAutenticavel->setLogin($login);
                $this->objAutenticavel->setSenha($senha);
                $autenticado = $this->CA->autenticar($this->objAutenticavel);       
                //var_dump($autenticado); break;
                if($autenticado){                       
                    redirect('/visita');                    
                }else{
                    redirect('/login');

                }

            } else {                
                redirect('/visita');
            }
        }

        //$this->index();
    }

    public function logado(){
        $dados['titulo'] = 'Logado';
        $this->template->load('default','area_restrita/tela_inicial.php',$dados);
    }

    public function acessoNegado(){
        $dados['titulo'] = 'Bloqueio';
        if(!$this->input->is_ajax_request()){
            $this->template->load('default','mensagem/sem_permissao.php',$dados);
        }else{
            print $this->load->view('mensagem/sem_permissao.php',$dados, true);
        }
    }
}
?>

second error

<!Doctype html>
<html>
<head></head>
<body class="no-skin login-layout">

        <div id="main-container" class="main-container login">

            <div class="main-content">
                <div class="main-content-inner">
                    <div class="page-content">
                        <?php
                        if (isset($_SESSION['alerta']) && !empty($_SESSION['alerta'])) {
                            print '<div class="alert alert-warning">
                                        <button data-dismiss="alert" class="close" type="button">
                                            <i class="ace-icon fa fa-times"></i>
                                        </button>' . $_SESSION['alerta'] .
                                    '</div>';
                        }
                        ?>
                        <?php
                        if (isset($_SESSION['erro']) && !empty($_SESSION['erro'])) {
                            print '<div class="alert alert-danger">
                                        <button data-dismiss="alert" class="close" type="button">
                                            <i class="ace-icon fa fa-times"></i>
                                        </button>' . $_SESSION['erro'] .
                                    '</div>';
                        }
                        ?>
                        <?= $body ?>
                    </div>
                    <!-- /.page-content -->
                </div>
            </div>
        </div>
    </body>
</html>

if anyone can help me I will be grateful!

  • 2
    Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – aynber Jan 23 '18 at 17:16

1 Answers1

0

public function index($dados=""){ $dados is not an array, it is a string, you cannot add values to it as if it were an array like $dados['titulo'] where [] denotes an array (hence the illegal string offset).

In your view you have <?= $body ?> which is never passed to your view in the index() function, hence your notice (it is also never echoed, fyi).

I'm not sure what $dados="" is supposed to be used for, but assuming its just a variable you want to pass to your controller you can do the following:

public function index($dados = "") {

    $data['body'] = 'Some body text';
    $data['somevarname'] = $dados;
    $data['titulo'] = 'Autenticação';
    //or
    /*
    $data = array(
        'body' => 'Some body text',
        'somevarname' => $dados,
        'titulo' => 'Autenticação'
    );
     * 
     */
    $this->template->load('login_template', 'area_restrita/acesso_login.php', $data);
}
Alex
  • 9,215
  • 8
  • 39
  • 82