Skip to content

Instantly share code, notes, and snippets.

@kamermans
Created August 13, 2014 00:08
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kamermans/27218b1a6dd61756b647 to your computer and use it in GitHub Desktop.
Save kamermans/27218b1a6dd61756b647 to your computer and use it in GitHub Desktop.
Replacement PHP session cleaner for Ubuntu (place inside /usr/lib/php5/)
#!/bin/sh
# check if file-based sessions are enabled
file_storage=$(/usr/lib/php5/sessionstorage | grep '^files$' -c)
if [ "$file_storage" = "0" ]; then
exit 0
fi
# first find all used files and touch them (hope it's not massive amount of files)
[ -x /usr/bin/lsof ] && /usr/bin/lsof -w -l +d "${1}" | awk -- '{ if (NR > 1) { print $9; } }' | xargs -i touch -c {}
# find all files older then maxlifetime
find "${1}" -depth -mindepth 1 -maxdepth 1 -ignore_readdir_race -type f -cmin "+${2}" -delete
exit 0
#!/bin/sh -e
# Utility script to determine which session storage mechanisms are used in php5
if which php5 >/dev/null 2>&1; then
# Iterate through the PHP files for each SAPI, using PHP to get the session.save_handler
session_handlers="";
for sapi in apache2 apache2filter cgi fpm; do
if [ -e /etc/php5/${sapi}/php.ini ]; then
cur=$(php5 -c /etc/php5/${sapi}/php.ini -d "error_reporting='~E_ALL'" -r 'echo ini_get("session.save_handler");')
[ -z "$cur" ] && continue
if [ -z "$session_handlers" ]; then
session_handlers="$cur"
else
session_handlers="$session_handlers\n$cur"
fi
fi
done
echo $session_handlers | sort -u
else
# Iterate through the PHP files for each SAPI, grepping out the session.save_handler
find /etc/php5/ -type f -name "*.ini" -print -not -path "/etc/php5/cli/*" \
| xargs grep -h session.save_handler \
| sed -n -e 's/^[[:space:]]*session.save_handler[[:space:]]*=[[:space:]]*\([[:alnum:]]\+\).*$/\1/p' \
| sort -u
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment