Problem with Session in CodeIgniter after putting site on new hosting

I have the following problem: when trying to perform login in some browsers or even on mobile (it does not occur in all, my friend for example can log in normally), the session simply does not start and the person remains disconnected.

There is no error code return. I've changed the session settings CodeIgniter to save in database, save in folder, and nothing solves.

I would like the help of those who eat CodeIgniter, because I have tested several things. I leave here some of my settings, to help:

Config.php

$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = NULL;
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;

After entering the form

I send what he typed in the 2 fields and send to the controller, in the controller I receive them like this:

$user = $this->input->post('user');
$senha = md5($this->input->post('senha'));

And send to model Login

$result = $this->login->entrar($user, $senha);

No model:

public function entrar($user, $senha){
        return $this->db->query('select *
                                 from usuario
                                 where (usuario = ? or email = ?)
                                 and senha = ?',array($user, $user, $senha))->row();
    }

If the return variable is not empty (get result with select) I write the session which is already started in CodeIgniter{[14] autoload]}

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

How do I record the session: ($result is the variable that returns with the user data)

$this->session->set_userdata('id', $result->id);
$this->session->set_userdata('nome_completo', $result->nome_completo);
$this->session->set_userdata('email', $result->email);
$this->session->set_userdata('usuario', $result->usuario);
$this->session->set_userdata('senha', $result->senha);

After that When I give the following command after recording session

print_r($this->session->userdata());

I get the session recorded correctly, but after reloading the page to update the user data on the screen the session is not set, and the print_r result is

array{}

Note: before my site was at the following address: http://meulivro-teste.000webhostapp.com, now I bought a hosting package on hostinger, and it is on the site: http://www.conteumahistoria.com .

Additional The PHP version was in 7.3 on hostinger, I went back to 7.1 to see if it fixed, but nothing changed

Author: VitorZF, 2019-03-27

1 answers

Solved as follows: (I will answer my own question in case someone comes to have the same problem can try to solve the same way I solved)

In the application/config/config folder.php

1-Remove the " _ " from the cookie name, errors may occur in some browsers

$config[‘sess_cookie_name’] = ‘ci_session’;

// Mude para:
$config[‘sess_cookie_name’] = ‘cisession’;

2-also change the cookie Part

$config['cookie_prefix'] = "";
$config['cookie_domain'] = ($_SERVER['SERVER_NAME'] == 'localhost' ? '' : preg_replace('/^www\./', '', $_SERVER['SERVER_NAME']));
$config['cookie_path'] = BASE_URI;

3-Change the following setting

$config['base_url'] = BASE_URL;

Now let's change the constants file.php inside the config folder

At the end of the file, add the following code

// Base URL (keeps this crazy sh*t out of the config.php
if (isset($_SERVER['HTTP_HOST']))
{
$base_url  = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ? 
'https' : 'http';
$base_url .= '://'. $_SERVER['HTTP_HOST'];
$base_url .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', 
$_SERVER['SCRIPT_NAME']);

// Base URI (It's different to base URL!)
$base_uri = parse_url($base_url, PHP_URL_PATH);

if (substr($base_uri, 0, 1) != '/')
{
    $base_uri = '/'.$base_uri;
}

if (substr($base_uri, -1, 1) != '/')
{
    $base_uri .= '/';
}
 }

else
{
 $base_url = 'http://localhost/';
 $base_uri = '/';
}

 // Define these values to be used later on
 define('BASE_URL', $base_url);
 define('BASE_URI', $base_uri);
 define('APPPATH_URI', BASE_URI.APPPATH);

 // We dont need these variables any more
  unset($base_uri, $base_url);

Appropriations a https://forum.codeigniter.com/thread-37828.html

 0
Author: VitorZF, 2019-04-03 12:38:59