session error in codeigniter?

Posted on

session error in codeigniter? – Here in this article, we will share some of the most common and frequently asked about PHP problem in programming with detailed answers and code samples. There’s nothing quite so frustrating as being faced with PHP errors and being unable to figure out what is preventing your website from functioning as it should like php and codeigniter . If you have an existing PHP-based website or application that is experiencing performance issues, let’s get thinking about session error in codeigniter?.

when I want to set session data in codeigniter 3 it says error like:

A PHP Error was encountered

Severity: Warning

Message: mkdir(): Invalid path

Filename: drivers/Session_files_driver.php

Line Number: 117

Backtrace:

File: C:xampphtdocsci-testapplicationcontrollerslogin.php
Line: 7
Function: __construct

File: C:xampphtdocsci-testindex.php
Line: 292
Function: require_once

Here is the code that where want to set session data.

$sess_array = array(
         'id' => 1,
         'username' => 'bikramkc.kc@gmail.com'
       );
$this->session->set_userdata($sess_array);

Solution :

Sharing a solution that helped me, try set your config variable like:

$config['sess_save_path'] = sys_get_temp_dir();

This error comes when we use directory bases session approach in CodeIgniter. If we use database based session approach error will go. Use code below –

change your application->config->config.php and set

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

& Run SQL query

CREATE TABLE IF NOT EXISTS `ci_sessions` (
        `id` varchar(40) NOT NULL,
        `ip_address` varchar(45) NOT NULL,
        `timestamp` int(10) unsigned DEFAULT 0 NOT NULL,
        `data` blob NOT NULL,
        PRIMARY KEY (id),
        KEY `ci_sessions_timestamp` (`timestamp`)
);

In config.php sess_save_path should be sys_get_temp_dir(); then it will resolve the error of mkdir(): Invalid path

The reason you encountered the error is because you didn’t have a $config['sess_save_path']

Go to you config.php and set

$config['sess_save_path'] = NULL;

1.Open the config.php on application/config folder

2.Browse all the way down to the $config['sess_save_path'] = NULL; line.

3.Change the NULL value to BASEPATH.'sessions'

It can be due to PHP and codeIgniter version.
For me, PHP 7.1.25 and CI 3.0.4 didn’t work. You can check with session_id(). Session was refreshed.
With CI 3.1.2 it works.

Try this in your config.php
$config['sess_save_path'] = NULL

  1. Use absolute path for $config['sess_save_path'] in config file.
  2. Point it to your writable temporary directory.
  3. Make sure the directory is under the directory allowed in apache or nginx config.

If $config['sess_save_path'] is sys_get_temp_dir() then session data will store in system folder.

And If you have database table as ci_sessions with below config.php:

$config['sess_driver'] = 'database';
$config['sess_cookie_name'] = 'ci_session';

The data will store in database. In this case you can $config['sess_save_path'] = NULL;

I had a similar case I have a multihost in godaddy and first I corrected the .htacces file

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase / 

    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c> 
    ErrorDocument 404 /index.php
</IfModule>

Then correct the file config.php

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

Leave a Reply

Your email address will not be published. Required fields are marked *