Welcome Guest, Not a member yet? Register   Sign In
how to keep session folder empty?
#1

Hello

I created a little cms for myself and use it as a light application.  I try to put it in an 100 Mega hosting. My sources are 60 Mega so at the begining all went fine. Then I forget the website. Recently  I found a 

Whoops!

We seem to have hit a snag. Please try again later...

After a while I found the problem . The session folder had so many files that my hostiong was full. Then I deleted them manually it works but after a while I need to delete them again.

What is your suggestion ? I don't need session actually. How to keep this folder empty
CodeIgniter 4.6
Reply
#2

Sessions are disabled by default. 
See config PHP https://www.php.net/manual/en/session.co...robability
Or configure cron to clear the folder
Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply
#3

[quote pid="421594" dateline="1728218028"]
I don't understand when I check my config I see

    public int $expiration = 7200;

But on the directory writable/session I have older session. I delete them by hand but why it is set 7200 and it will not delete ? 7200 seconds is 120 mn and I find session older? 

How to clean this directory ?
how to keeps sessions only temporary ?
    public int $expiration = 7200;

    public int $expiration = 7200;

  public int $expiration = 7200;
[/quote]
CodeIgniter 4.6
Reply
#4

(This post was last modified: 10-12-2024, 03:16 AM by InsiteFX.)

Set it to 0 and it should delete them when the web browser closes.

NOTE:

PHP.INI
Code:
; NOTE: If you are using the subdirectory option for storing session files
;      (see session.save_path above), then garbage collection does *not*
;      happen automatically.  You will need to do your own garbage
;      collection through a shell script, cron entry, or some other method.
;      For example, the following script is the equivalent of setting
;      session.gc_maxlifetime to 1440 (1440 seconds = 24 minutes):
;          find /path/to/sessions -cmin +24 -type f | xargs rm
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

(This post was last modified: 10-12-2024, 03:28 AM by cb21.)

(10-12-2024, 03:00 AM)InsiteFX Wrote: Set it to 0 and it should delete them when the web browser closes.

NOTE:

PHP.INI
Code:
; NOTE: If you are using the subdirectory option for storing session files
;      (see session.save_path above), then garbage collection does *not*
;      happen automatically.  You will need to do your own garbage
;      collection through a shell script, cron entry, or some other method.
;      For example, the following script is the equivalent of setting
;      session.gc_maxlifetime to 1440 (1440 seconds = 24 minutes):
;          find /path/to/sessions -cmin +24 -type f | xargs rm
I tryed to set 
      public int $expiration = 0;
But it does not help
      public int $expiration = 0;

I am on a share hosting.  then no access to php.ini  hmmm I will try to write a php code that should be run each time someone visite any page. If I can have access to this directory (( N ot really easy.  I expected that a config exists for that.  I don't need any session actually.
CodeIgniter 4.6
Reply
#6

(This post was last modified: 10-12-2024, 05:04 AM by InsiteFX.)

You can try these Helper methods that I created to clear out writable/logs & writable/session files on localhost.

PHP Code:
<?php

declare(strict_types=1);

/**
 * app/Helpers/utility_helper
 *
 */

/**
 *  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");
 }
}

// End of utility_helper Helper.

/**
 * -----------------------------------------------------------------------
 * Filename: utility_helper.php
 * Location: ./app/Helpers/utility_helper.php
 * -----------------------------------------------------------------------
 */ 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#7

I will do something like your example. I just realise that on other site, sessions or logs can be a huge number. It is not clean to keep a directory with too many files.  I have 9000 in an other site . I never check it because all was correct but I should clean that on all my sites. 
I am amazed that codeigniter never do something automatic for that.
CodeIgniter 4.6
Reply
#8

This does work on localhost, I have not checked it on a live server, it may need re-coding for that.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#9

(This post was last modified: 10-13-2024, 09:22 AM by captain-sensible. Edit Reason: As oliver twist once said " more" )

A simple bash script is all i am using , via cron job facility via cPanel.If you have and its quite common , cPanel them shimmy down to cron jobs.



[Image: cPanelDash.png]


I have a simple bash script :

Code:
#!/bin/bash

rm  /home/rzuserf3/andrinadesignstudio.com/writable/session/ci_*

the script is called clean.sh  and is at the root of one of my web apps on shared hosting .

On localhost i tested and you need the full path. Same for live hosting although you might have to play with it. Full path needs to be in code of cron job and in bash script .


in cron tab i just selcted in common settings hourly ,and that auto filled in everything

[Image: cronJob.png]



i did have in cron tab before

Code:
/usr/local/bin/bash /home/rzuserf3/andrinadesignstudio.com/clean.sh

but hosting came back and said cron  autoloads bash . Its working live fine. Now with the web i have session only is concerned to a single admin user; now if they were logged in and session removed i guess they will be dropped out of session , with consequences ? So i will tweak script at some point to remove session older than a day ..
CMS CI4 A CMS system, runs out of the box written on top of CI4
Arch Book  CodeIgniter4 on Apache(pages 92-114) 
Reply




Theme © iAndrew 2016 - Forum software by © MyBB