Edit File: clean_user_php_sessions
#!/usr/local/cpanel/3rdparty/bin/perl # cpanel - scripts/clean_user_php_sessions Copyright 2017 cPanel, Inc. # All rights Reserved. # copyright@cpanel.net http://cpanel.net # This code is subject to the cPanel license. Unauthorized copying is prohibited package scripts::clean_user_php_sessions; use strict; use Cpanel::ProgLang::Supported::php::Ini (); use Cpanel::ProgLang (); use Cpanel::EtcCpanel (); use Cpanel::PHPINI (); if ( !caller() ) { main(@ARGV); } sub main { my ($args) = @_; help() if $args; if ( -e "$Cpanel::EtcCpanel::ETC_CPANEL_DIR/ea4/is_ea4" ) { my $php = Cpanel::ProgLang->new( type => 'php' ); my $packages = $php->get_installed_packages(); foreach my $pack ( @{$packages} ) { my $ini = $php->get_ini( 'package' => $pack ); my $directives = $ini->get_basic_directives(); my ( $path, $maxlifetime ); foreach my $directive ( @{$directives} ) { $maxlifetime = $directive->{'value'} if $directive->{'key'} eq 'session.gc_maxlifetime'; $path = $directive->{'value'} if $directive->{'key'} eq 'session.save_path'; } clean_sessions( $path, $maxlifetime ); } } else { my $dirs = Cpanel::PHPINI::get_directives( [ 'session.save_path', 'session.gc_maxlifetime' ], 1, '/usr/local' ); clean_sessions( $dirs->{'session.save_path'}{'value'}, $dirs->{'session.gc_maxlifetime'}{'value'} ); } return 1; } sub clean_sessions { my ( $path, $maxlife ) = @_; # session.save_path could be commented out, in which case we will let PHP handle garbage collection. return 0 if !defined $path || !-d $path; $maxlife = $Cpanel::ProgLang::Supported::php::Ini::SESSION_MAXLIFETIME if !defined $maxlife; # get_basic_directives can return values with leading/trailing whitespace. s/^\s+|\s+$//g for ( $path, $maxlife ); $maxlife = $Cpanel::ProgLang::Supported::php::Ini::SESSION_MAXLIFETIME if $maxlife !~ /^\d+$/; my $time = time; opendir( my $dh, $path ) or die "Could not open directory $path: $!"; while ( my $file = readdir $dh ) { next if $file !~ m/^sess_[a-z0-9]+$/; my $ctime = ( stat("$path/$file") )[10]; unlink "$path/$file" if $time - $ctime > $maxlife; } return 1; } sub help { print <<USAGE; $0 [--help] Clean expired PHP session files. PHP session files can be stored in a secure temporary directory. This removes the ability for PHP to clean up its own session files. This script should be run at least once a day to remove any old PHP session files. USAGE exit 1; } 1;