Problem with Session on Codeigniter after putting website into new hosting

Asked

Viewed 720 times

1

I have the following problem: when trying to accomplish the logon in some browsers or even on mobile phone (it does not occur at all, my friend for example can log in normally), the session simply is not started and the person remains unattended.

No error code return. I have changed the session settings for CodeIgniter to save to database, save to folder, and nothing solves.

I would like the help of 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 typing the form

Send what he typed in the 2 fields and send to the controller, in the controller I get 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);

In the 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 output with select) I save the session that is already started in Codeigniter autoload

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

How I record the session: ($result is the variable that comes back with 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 saving the session

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

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

array{}

OBS: Before my website was at the following address: http://meulivro-teste.000webhostapp.com, now I bought a package of accommodation at 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

  • It is not possible to know if you are loading the library in the correct way. It is not possible to know in which way the method is recovering the data. It is not possible to know which variables you use to validate the session. You can’t know how the session is validated in the browser (since you report that in some, it works). Show the code. Show minimally how to record, retrieve and validate the session.

  • I will edit and complement with the data you spoke

  • You have already tried to load librarie Session in the autoload.php configuration file?

  • She’s pressing the autoload, like I showed you up there

  • Basically vc confirms if user and password informed exist in the database, and if true, retrieves the user data from the database and writes them in the session. But what’s the Function/method validating the data recorded in the session? This is the problem. It is not enough to write a session with the user data, the php (or the browser) has to know what these data are for, if they are still valid or if they are exploited, etc.

  • I use the $this->Session->has_userdata('usenomequesalvei') method that is the default for codeigniter, if there is something saved as in the session’s'usenomequesalvei' field it returns true

  • Possible duplicate of PHP Controller with Codeigniter

  • This method does not serve for this, and is already obsolete in the new version of CodeIgniter. The has_userdata($key) will only confirm that a session item exists, but does not control authentication. I believe your question is a duplicate of this one: https://answall.com/questions/180083/controle-de-sessions-php-com-codeigniter. That’s exactly what you’re looking for...

  • But the problem is not in validating the session, but rather that the session is not set, soon after logging in I give print_r throughout the session and the session appears, but when resetting the page and give print_r($this->Session->userdata()) there is nothing else in the session

Show 4 more comments

1 answer

0


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.php folder

1 - Removing "_" from 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.php file 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);

CREDITS TO https://forum.codeigniter.com/thread-37828.html

Browser other questions tagged

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