What is the default location of session files on an installation of Apache/PHP on Ubuntu 10.10?
The default session.save_path is set to ""
which will evaluate to your system's temp directory. See this comment at https://bugs.php.net/bug.php?id=26757 stating:
The new default for save_path in upcoming releaess (sic) will be the empty string, which causes the temporary directory to be probed.
You can use sys_get_temp_dir to return the directory path used for temporary files
To find the current session save path, you can use
Refer to this answer to find out what the temp path is when this function returns an empty string.
Depending on the type of server you're running it can vary. To locate the directory, search for the following in your php.ini file.
upload_tmp_dir = "c:/wamp/tmp"
The directory can be different for you.
Non of the above worked for me using the IUS repo for CentOS 7 with PHP 7.2:
php -v
> PHP 7.2.30 (cli) (built: Apr 19 2020 00:32:29) ( NTS )
php -r 'echo session_save_path(), "\n";
>
php -r 'echo sys_get_temp_dir(), "\n";'
> /tmp
However, sessions weren't saved in the /tmp
folder, but in the /var/lib/php/mod_php/session/
folder:
ls /var/lib/php/mod_php/session/
> sess_3cebqoq314pcnc2jgqiu840h0k sess_ck5dtaerol28fpctj6nutbn6fn sess_i24lgt2v2l58op5kfmj1k6qb3h sess_nek5q1alop8fkt84gliie91703
> sess_9ff74f4q5ihccnv6com2a8409t sess_dvrt9fmfuolr8bqt9efdpcbj0d sess_igdaksn26hm1s5nfvtjfb53pl7 sess_tgf5b7gkgno8kuvl966l9ce7nn
I had the same trouble finding out the correct path for sessions on a Mac. All in all, I found out that the CLI PHP has a different temporary directory than the Apache module: Apache used /var/tmp
, while CLI used something like /var/folders/kf/hk_dyn7s2z9bh7y_j59cmb3m0000gn/T
. But both ways, sys_get_temp_dir()
got me the right path when session.save_path
is empty. Using PHP 5.5.4.
I believe its in /tmp/. Check your phpinfo function though, it should say session.save_path in there somewhere.
If unsure of compiled default for session.save_path
, look at the pertinent php.ini
.
Normally, this will show the commented out default value.
Ubuntu/Debian old/new php.ini
locations:
Older php5 with Apache: /etc/php5/apache2/php.ini
Older php5 with NGINX+FPM: /etc/php5/fpm/php.ini
Ubuntu 16+ with Apache: /etc/php/*/apache2/php.ini
*
Ubuntu 16+ with NGINX+FPM - /etc/php/*/fpm/php.ini
*
* /*/
= the current PHP version(s) installed on system.
To show the PHP version in use under Apache:
$ a2query -m | grep "php" | grep -Eo "[0-9]+\.[0-9]+"
7.3
Since PHP 7.3 is the version running for this example, you would use that for the php.ini
:
$ grep "session.save_path" /etc/php/7.3/apache2/php.ini
;session.save_path = "/var/lib/php/sessions"
Or, combined one-liner:
$ APACHEPHPVER=$(a2query -m | grep "php" | grep -Eo "[0-9]+\.[0-9]+") \ && grep ";session.save_path" /etc/php/${APACHEPHPVER}/apache2/php.ini
Result:
;session.save_path = "/var/lib/php/sessions"
Or, use PHP itself to grab the value using the "cli" environment (see NOTE below):
$ php -r 'echo session_save_path() . "\n";'
/var/lib/php/sessions
$
These will also work:
php -i | grep session.save_path
php -r 'echo phpinfo();' | grep session.save_path
NOTE:
The 'cli' (command line) version of php.ini
normally has the same default values as the Apache2/FPM versions (at least as far as the session.save_path
). You could also use a similar command to echo the web server's current PHP module settings to a webpage and use wget/curl to grab the info. There are many posts regarding phpinfo()
use in this regard. But, it is quicker to just use the PHP interface or grep
for it in the correct php.ini
to show it's default value.
EDIT: Per @aesede comment -> Added php -i
. Thanks