CodeIgniter Forums
Session files - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Session files (/showthread.php?tid=92024)



Session files - paulkd - 11-16-2024

Hi All,

I recently saw that I had a lot of session files and wonder if the amount being created by CodeIgniter is correct. So I installed a fresh version of 4.5.5 app starter and made the following changes:-

Controller - home.php

PHP Code:
class Home extends BaseController
{
  public function index(): string
  
{
    $session service('session');
    return view('welcome_message');
  }


View - welcome_message.php

PHP Code:
<!DOCTYPE html>
<
html lang="en">
<
head>
    <meta charset="UTF-8">
    <title>Welcome to CodeIgniter 4!</title>
    <meta http-equiv="refresh" content="60"

CodeIgniter is creating a new session file every 5 minutes. I assume this is because of
Code:
public int $timeToUpdate = 300;
in Config/Session.php

Is this (many files) the expected behaviour? and if so does it make
Code:
$expiration = 7200;
redundant?

ELI5  Angel

[Image: IknZNroT]


RE: Session files - InsiteFX - 11-18-2024

Here are 2 helper method's that I wrote to clear out session and log files in the writable folder.
Do not use on a live sever, for a live server please use a corn job to delete them.

PHP Code:
/**
 *  clearSessionFiles ()
 * -----------------------------------------------------------------------
 */
if ( ! function_exists('clearSessionFiles'))
{
 
/**
 * clearSessionFiles ()
 * -------------------------------------------------------------------
 *
 */
 
function clearSessionFiles(): void
 
{
 
// Counter for number of session files - dir . & .. and index.html
 
$count 0;

 
// Get all files in our session folder
 
$listFile scandir("../writable/session/");
 foreach (
$listFile as $file) {
 
// if the file is a . or .. directory skip it!
 
if ( ! is_dir("../writable/session/" $file)) {
 
$count++;
 }

 
// We now have all session files and will exclude the index.html file
 
if (! is_dir("../writable/session/" $file)) {
 if (
$file !== "index.html") {
 
// for debugging count of files
 //echo $count . " " . $file . "<br>";

 // unlink and delete the files
 
unlink("../writable/session/" $file);
 }
 }
 }

 
// Close the session, it will re-intialize itself.
 
session()->close();

 
// Log message
 
log_message('debug'"clearSessionFiles() - Deleted " $count " Session files");
 }
}

/**
 *  clearLogFiles ()
 * -----------------------------------------------------------------------
 */
if ( ! function_exists('clearLogFiles'))
{
 
/**
 * clearLogFiles ()
 * -------------------------------------------------------------------
 *
 */
 
function clearLogFiles(): void
 
{
 
// Counter for number of log files - dir . & .. and index.html
 
$count 0;

 
// Get all files in our logs folder
 
$listFile scandir("../writable/logs/");
 foreach (
$listFile as $file) {
 
// if the file is a . or .. directory skip it!
 
if ( ! is_dir("../writable/logs/" $file)) {
 
$count++;
 }

 
// We now have all log files and will exclude the index.html file
 
if (!is_dir("../writable/logs/" $file)) {
 if (
$file !== "index.html") {
 
// for debugging count of files
 //echo $count . " " . $file . "<br>";

 // unlink and delete the log files
 
unlink("../writable/logs/" $file);
 }
 }
 }

 
// Log message
 
log_message('debug'"clearLogFiles() - Deleted " $count " Log files");
 }




RE: Session files - paulkd - 11-19-2024

Hi InsiteFX,

Thanks for the code. I've also updated this line in app/Config/Session.php

Code:
public bool $regenerateDestroy = true;



RE: Session files - InsiteFX - 11-19-2024

Here is the info that I found on it.

cleanup php session files