cleanup php session files
PHP sessions default to file-based storage on many shared hosting environments, which means each session is a physical file in a dedicated directory. While this works out of the box, it introduces a significant maintenance burden: those files are rarely deleted automatically if the application logic doesn't explicitly destroy them or if the server's garbage collection fails. In your case, having 145,000 session files accumulate over several months indicates that the built-in cleanup mechanism is either not triggering as expected or is being overwhelmed by new sessions faster than old ones are removed.
The core mechanism for cleaning up these files relies on two factors: the gc_probability and gc_maxlifetime settings in your php.ini. PHP does not delete session files immediately when a user logs out; instead, it uses a probabilistic garbage collector that runs on every request. If a file's modification time exceeds session.gc_maxlifetime (default is usually 1440 seconds) and the random chance check passes based on gc_probability, the file is deleted.
If your files are persisting indefinitely despite this logic, the most common culprit is the ownership of those files. You mentioned that you have FTP access but the session directory belongs to the web server process (often www-data or apache). If your application code attempts to delete these files using standard PHP functions like unlink() or session_destroy(), and the script runs under a different user context than the one that owns the files, the deletion will silently fail. Furthermore, if you are relying on the server-side garbage collector, you need to ensure that session.gc_maxlifetime is actually set correctly in your php.ini and that the web server can write back to that directory after a file is marked for deletion.
Given your constraint of having no command-line access and being unable to change the ownership of the session directory, relying solely on the PHP internal garbage collector might not be sufficient if the files are not expiring correctly in the first place. However, you can implement a robust solution directly within your application code without needing server-level cron jobs.
You can create a dedicated maintenance function that runs periodically from your frontend (e.g., via an AJAX call or a route) or from a local cron job on your home machine that triggers your site. This approach bypasses the need for direct file system access by leveraging PHP's session management APIs. The key is to ensure that when a user logs out, their session is explicitly destroyed before any cleanup logic runs, and then force a garbage collection cycle.
Here is a pattern you can implement in your application:
function cleanupOldSessions() {
// Ensure we are using file storage, which is common on shared hosts
ini_set('session.save_handler', 'files');
// Set the max lifetime to ensure old files are eligible for deletion
// This value must match or be less than your php.ini setting
ini_set('session.gc_maxlifetime', 86400); // 24 hours
// Increase the probability of garbage collection running on this request
ini_set('session.gc_probability', 1);
ini_set('session.gc_divisor', 100); // 1% chance per request
// Destroy any active sessions if needed, or rely on the probabilistic GC
session_start();
// Force a garbage collection cycle immediately
session_gc();
}
While calling cleanupOldSessions() via a browser request works, it adds latency to every visit until the function is hit. A better approach for high-volume sites is to create a lightweight public endpoint or API route that triggers this logic only when necessary, perhaps triggered by a local cron job on your home machine that pings your site once a day.
Alternatively, if you can modify the PHP configuration for your specific domain via cPanel or similar control panels, check the session.gc_maxlifetime value there. If it is set to 0, garbage collection is disabled entirely, which would explain why files never disappear. Setting this to a reasonable duration (like 1440 seconds) and ensuring the session.save_path is writable by the web server user usually resolves the accumulation issue without needing custom code.
If you must rely on custom scripts due to configuration locks, ensure that your cleanup script explicitly calls session_gc() after starting a session. Do not assume that logging out a user automatically triggers file deletion; the probabilistic nature of PHP means it only happens when the internal counter hits the threshold defined by gc_probability. By combining correct server-side settings with an application-level trigger that forces garbage collection, you can keep your session directory manageable without needing root access or command-line tools.
