session.cache_limiter may be empty string to disable cache headers entirely.
Quote:
> Setting the cache limiter to '' will turn off automatic sending of cache headers entirely.
http://php.net/manual/en/function.session-cache-limiter.php
这些函数的行为受 php.ini 中的设置影响。
名字 | 默认 | 可修改范围 | 更新日志 |
---|---|---|---|
session.save_path | "" | PHP_INI_ALL | |
session.name | "PHPSESSID" | PHP_INI_ALL | |
session.save_handler | "files" | PHP_INI_ALL | |
session.auto_start | "0" | PHP_INI_PERDIR | |
session.gc_probability | "1" | PHP_INI_ALL | |
session.gc_divisor | "100" | PHP_INI_ALL | |
session.gc_maxlifetime | "1440" | PHP_INI_ALL | |
session.serialize_handler | "php" | PHP_INI_ALL | |
session.cookie_lifetime | "0" | PHP_INI_ALL | |
session.cookie_path | "/" | PHP_INI_ALL | |
session.cookie_domain | "" | PHP_INI_ALL | |
session.cookie_secure | "" | PHP_INI_ALL | |
session.cookie_httponly | "" | PHP_INI_ALL | 自 PHP 5.2.0 起有效 |
session.cookie_samesite | "" | PHP_INI_ALL | 自 PHP 7.3.0 起有效 |
session.use_strict_mode | "0" | PHP_INI_ALL | 自 PHP 5.5.2 起有效 |
session.use_cookies | "1" | PHP_INI_ALL | |
session.use_only_cookies | "1" | PHP_INI_ALL | |
session.referer_check | "" | PHP_INI_ALL | |
session.cache_limiter | "nocache" | PHP_INI_ALL | |
session.cache_expire | "180" | PHP_INI_ALL | |
session.use_trans_sid | "0" | PHP_INI_ALL | |
session.trans_sid_tags | "a=href,area=href,frame=src,form=" | PHP_INI_ALL | 自 PHP 7.1.0 起有效。 |
session.trans_sid_hosts | $_SERVER['HTTP_HOST'] | PHP_INI_ALL | 自 PHP 7.1.0 起有效。 |
session.sid_length | "32" | PHP_INI_ALL | 自 PHP 7.1.0 起有效。 |
session.sid_bits_per_character | "4" | PHP_INI_ALL | 自 PHP 7.1.0 起有效。 |
session.upload_progress.enabled | "1" | PHP_INI_PERDIR | 自 PHP 5.4.0 起有效。 |
session.upload_progress.cleanup | "1" | PHP_INI_PERDIR | 自 PHP 5.4.0 起有效。 |
session.upload_progress.prefix | "upload_progress_" | PHP_INI_PERDIR | 自 PHP 5.4.0 起有效。 |
session.upload_progress.name | "PHP_SESSION_UPLOAD_PROGRESS" | PHP_INI_PERDIR | 自 PHP 5.4.0 起有效。 |
session.upload_progress.freq | "1%" | PHP_INI_PERDIR | 自 PHP 5.4.0 起有效。 |
session.upload_progress.min_freq | "1" | PHP_INI_PERDIR | 自 PHP 5.4.0 起有效。 |
session.lazy_write | "1" | PHP_INI_ALL | 自 PHP 7.0.0 起有效。 |
url_rewriter.tags | "a=href,area=href,frame=src,form=" | PHP_INI_ALL | 自 PHP 7.1.0 起,session 功能不再使用此选项。 |
session.hash_function | "0" | PHP_INI_ALL | 自 PHP 7.1.0 起移除。 |
session.hash_bits_per_character | "4" | PHP_INI_ALL | 自 PHP 7.1.0 起移除。 |
session.entropy_file | "" | PHP_INI_ALL | 自 PHP 7.1.0 起移除。 |
session.entropy_length | "0" | PHP_INI_ALL | 自 PHP 7.1.0 起移除。 |
session.bug_compat_42 | "1" | PHP_INI_ALL | 自 PHP 5.4.0 起移除。 |
session.bug_compat_warn | "1" | PHP_INI_ALL | 自 PHP 5.4.0 起移除。 |
会话管理系统支持许多配置选项,可以在自己的 php.ini 文件中设定。这里只是个简短的概览。
session.save_handler
string
session.save_path
string
此指令还有一个可选的 N 参数来决定会话文件分布的目录深度。例如,设定为 '5;/tmp' 将使创建的会话文件和路径类似于 /tmp/4/b/1/e/3/sess_4b1e384ad74619bd212e236e52a5a174If。要使用 N 参数,必须在使用前先创建好这些目录。在 ext/session 目录下有个小的 shell 脚本名叫 mod_files.sh,windows 版本是 mod_files.bat 可以用来做这件事。此外注意如果使用了 N 参数并且大于 0,那么将不会执行自动垃圾回收,更多信息见 php.ini。另外如果用了 N 参数,要确保将 session.save_path 的值用双引号 "quotes" 括起来,因为分隔符分号( ;)在 php.ini 中也是注释符号。
文件储存模块默认使用 mode 600 创建文件。通过 修改可选参数 MODE 来改变这种默认行为: N;MODE;/path ,其中 MODE 是 mode 的八进制表示。 MODE 设置不影响进程的掩码(umask)。
如果将此设定为一个全局可读的目录,例如 /tmp(默认值),服务器上的其他用户有可能通过该目录的文件列表破解会话。
使用以上描述的可选目录层级参数 N 时请注意,对于绝大多数站点,大于1或者2的值会不太合适——因为这需要创建大量的目录:例如,值设置为 3 需要在文件系统上创建 64^3 个目录,将浪费很多空间和 inode。
仅仅在绝对肯定站点足够大时,才可以设置 N 大于2。
Note: 在 PHP 4.3.6 之前,Windows 用户必须修改此选项以使用 PHP 的会话函数。必须指定一个合法路径,例如:c:/temp。
session.name
string
session.auto_start
boolean
session.serialize_handler
string
session.gc_probability
integer
session.gc_divisor
integer
session.gc_maxlifetime
integer
Note:
如果不同的脚本具有不同的 session.gc_maxlifetime 数值但是共享了同一个地方存储会话数据,则具有最小数值的脚本会清理数据。此情况下,与 session.save_path 一起使用本指令。
session.referer_check
string
session.entropy_file
string
Note: 自 PHP 5.4.0 起,默认情况下, session.entropy_file 在 /dev/urandom 或 /dev/arandom 可用的时候使用它们。 在 PHP 5.3.0 中此指令默认留空。
session.entropy_length
integer
session.use_strict_mode
boolean
Note:
The expiration timestamp is set relative to the server time, which is not necessarily the same as the time in the client's browser.
session.cache_limiter
string
session.cache_expire
integer
session.use_trans_sid
boolean
Note: 基于 URL 的会话管理比基于 cookie 的会话管理有更多安全风险。例如用户有可能通过 email 将一个包含有效的会话 ID 的 URL 发给他的朋友,或者用户总是有可能在收藏夹中存有一个包含会话 ID 的 URL 来以同样的会话 ID 去访问站点。 自 PHP 7.1.0 开始,透明 SID 开始使用完整的 URL 绝对路径,例如 https://php.net/。 在此之前 PHP 只会使用相对路径。使用 session.trans_sid_hosts 定义重写的目标 host。
Note: Before PHP 7.1.0, url_rewriter.tags was used for this purpose. Since PHP 7.1.0, fieldset is no longer considered as special tag.
session.trans_sid_hosts
string
session.bug_compat_42
boolean
Note: PHP 5.4.0 中已经移除。
session.bug_compat_warn
boolean
Note: PHP 5.4.0 中已经移除。
session.sid_length
integer
Compatibility Note: Use 32 instead of session.hash_function=0 (MD5) and session.hash_bits_per_character=4, session.hash_function=1 (SHA1) and session.hash_bits_per_character=6. Use 26 instead of session.hash_function=0 (MD5) and session.hash_bits_per_character=5. Use 22 instead of session.hash_function=0 (MD5) and session.hash_bits_per_character=6. You must configure INI values to have at least 128 bits in session ID. Do not forget to set an appropriate value for session.sid_bits_per_character, otherwise you will have weaker session ID.
Note: This setting is introduced in PHP 7.1.0.
session.sid_bits_per_character
integer
Note: This setting is introduced in PHP 7.1.0.
Note: 如果要符合 XHTML,去掉 form 项并在表单字段前后加上 <fieldset> 标记。
session.hash_function
mixed
Since PHP 5.3.0 it is also possible to specify any of the algorithms provided by the hash extension (if it is available), like sha512 or whirlpool. A complete list of supported algorithms can be obtained with the hash_algos() function.
Note:
这是 PHP 5 引进的。
session.hash_bits_per_character
integer
Note:
这是 PHP 5 引进的。
session.upload_progress.enabled
boolean
session.upload_progress.cleanup
boolean
Note: It is highly recommended to keep this feature enabled.
session.upload_progress.prefix
string
session.upload_progress.name
string
session.upload_progress.freq
mixed
session.upload_progress.min_freq
integer
session.lazy_write
boolean
register_globals 配置选项影响到会话变量是怎样存储和恢复的。
Upload progress will not be registered unless session.upload_progress.enabled is enabled, and the $_POST[ini_get("session.upload_progress.name")] variable is set. See Session Upload Progress for mor details on this functionality.
session.cache_limiter may be empty string to disable cache headers entirely.
Quote:
> Setting the cache limiter to '' will turn off automatic sending of cache headers entirely.
http://php.net/manual/en/function.session-cache-limiter.php
Use SessionHandlerInterface interface Custom redis session, found the following:
Use ini_set ('session.save_path', "tcp: //127.0.0.1: 6379? Auth = password"); will be reported:
PHP Fatal error: session_start (): Failed to initialize storage module: user (path: tcp: //127.0.0.1: 6379? Auth = password);
Using session_save_path ("tcp: //127.0.0.1: 6379? Auth = password") will not
session.use_strict_mode does very little to strengthen your security: only one very specific variant of attack is migitated by this (where the attacker hands an "empty" sid to the victim to adapt his own browser to that session later) - versus for example the case where he pre-opens a session, handing the sid of that one to the victim, so the victim gets adapted to the pre-opened session. In the latter case this flag does nothing to help. In every other scenario with other vulnerabilities where the session id gets leaked, the flag helps nigher.
But this flag renders the php function session_id() useless in its parameterized variant, thus preventing any php functionality that builds upon this function.
You should take more care configuring session.gc_maxlifetime when virtual hosts share the same session-saving directory. One host's session data may be gc'ed when another host runs php.
In response to this comment: http://php.net/manual/en/session.configuration.php#107990 where it is claimed that gc_maxlifetime values larger than 65535 break the session system. I cannot reproduce this.
I've set gc_maxlifetime to 31536000 (1 year) and the session system works just fine. I haven't tried how long a session lasts now (I'm in the process of testing this), but it certainly doesn't break PHP sessions.
I found out that if you need to set custom session settings, you only need to do it once when session starts. Then session maintains its settings, even if you use ini_set and change them, original session still will use it's original setting until it expires.
Just thought it might be useful to someone.
We found a session.save_path depth of 3 led to excessive wastage of inodes and in fact disk space in storing the directory tree. dir_indexes option on ext2/3/4 makes larger directories more feasible anyway, so we decided to move to a depth of 2 instead.
It took a little puzzling to figure out how to move the existing PHP sessions up one directory tree, but we ended up running this in the root sessions directory:
#!/bin/sh
for a in ./* ; do
cd ./$a
pwd
for b in ./* ; do
cd ./$b
pwd
# Move existing sessions out
find ./* -xdev -type f -print0 | xargs -0 mv -t .
# Remove subdirectories
find ./* -xdev -type d -print0 | xargs -0 rmdir
cd ..
done
cd ..
done
This script may not be the best way to do it, but it got the job done fast. You can modify it for different depths by adding or removing "for" loops.
The documentation gives a depth of 5 as an example, but five is right out. If you're going beyond 2, you're at the scale where you may want to to look at a large memcached or redis instance instead.
On debian (based) systems, changing session.gc_maxlifetime at runtime has no real effect. Debian disables PHP's own garbage collector by setting session.gc_probability=0. Instead it has a cronjob running every 30 minutes (see /etc/cron.d/php5) that cleans up old sessions. This cronjob basically looks into your php.ini and uses the value of session.gc_maxlifetime there to decide which sessions to clean (see /usr/lib/php5/maxlifetime).
You can adjust the global value in your php.ini (usually /etc/php5/apache2/php.ini). Or you can change the session.save_path so debian's cronjob will not clean up your sessions anymore. Then you need to either do your own garbage collection with your own cronjob or enable PHP's garbage collection (php then needs sufficient privileges on the save_path).
Why does Debian not use PHP's garbarage collection?
For security reasons, they store session data in a place (/var/lib/php5) with very stringent permissions. With the sticky bit set, only root is allowed to rename or delete files there, so PHP itself cannot clean up old session data. See https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=267720 .
Being unable to find an actual copy of mod_files.sh, and seeing lots of complaints/bug fix requests for it, here's one that works. It gets all its parameters from PHP.INI, so you don't have the opportunity to mess up:
#!/bin/bash
#
# Creates directories for PHP session storage.
# Replaces the one that "comes with" PHP, which (a) doesn't always come with it
# and (b) doesn't work so great.
#
# This version takes no parameters, and uses the values in PHP.INI (if it
# can find it).
#
# Works in OS-X and CentOS (and probably all other) Linux.
#
# Feb '13 by Jeff Levene.
[[ $# -gt 0 ]] && echo "$0 requires NO command-line parameters.
It gets does whatever is called for in the PHP.INI file (if it can find it).
" && exit 1
# Find the PHP.INI file, if possible:
phpIni=/usr/local/lib/php.ini # Default PHP.INI location
[[ ! -f "$phpIni" ]] && phpIni=/etc/php.ini # Secondary location
[[ ! -f "$phpIni" ]] && phpIni= # Found it?
# Outputs the given (as $1) parameter from the PHP.INI file:
# The "empty" brackets have a SPACE and a TAB in them.
#
PhpConfigParam() {
[[ ! "$phpIni" ]] && return
# Get the line from the INI file:
varLine=`grep "^[ ]*$1[ ]*=" "$phpIni"`
# Extract the value:
value=`expr "$varLine" : ".*$1[ ]*=[ ]*['\"]*\([^'\"]*\)"`
echo "$value"
}
if [[ "$phpIni" ]]
then
savePath=`PhpConfigParam session.save_path`
# If there's a number and semicolon at the front, remove them:
dirDepth=`expr "$savePath" : '\([0-9]*\)'`
[[ "$dirDepth" ]] && savePath=`expr "$savePath" : '[0-9]*;\(.*\)'` || dirDepth=0
bits=`PhpConfigParam session.hash_bits_per_character`
case "x$bits" in
x) echo "hash_bits_per_character not defined. Not running." ; exit 2 ;;
x4) alphabet='0 1 2 3 4 5 6 7 8 9 a b c d e f' ;;
x5) alphabet='0 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t u v' ;;
x6) alphabet='0 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t u v'
alphabet="$alphabet w x y z A B C D E F G H I J K L M N O P Q R S T U V W"
alphabet="$alphabet X Y Z - ,"
;;
*) echo "unrecognized hash_bits_per_character. Not running." ; exit 2 ;;
esac
else
echo "Cannot find the PHP.INI file. Not running. Sorry."
exit 2
fi
# The depth of directories to create is $1. 0 means just create the named
# directory. Directory to start with is $2.
#
# Used recursively, so variables must be "local".
doDir() {
local dir="$2"
if [[ -d "$dir" ]]
then
echo "Directory '$dir' already exists. No problem."
elif [[ -f "$dir" ]]
then
echo "FILE '$dir' exists. Aborting." ; exit 2
else
if mkdir "$dir"
then
echo "Directory '$dir' created."
else
echo "Cannot create directory '$dir'. Aborting." ; exit 2
fi
fi
chmod a+rwx "$dir"
if [[ $1 -gt 0 ]]
then
local depth=$(( $1 - 1 ))
for letter in $alphabet
do doDir $depth "$dir/$letter"
done
fi
}
echo "Running with savePath='$savePath', dirDepth=$dirDepth, and bitsPerCharacter=$bits."
sleep 3
doDir $dirDepth "$savePath"
exit 0
max value for "session.gc_maxlifetime" is 65535. values bigger than this may cause php session stops working.
This is how I set my session.save_path
session.save_path = "1;/home/askapache/tmp/s"
So to create the folder structure you can use this compatible shell script, if you want to create with 777 permissions change the umask to 0000;
sh -o braceexpand -c "umask 0077;mkdir -p s/{0..9}/{a..z} s/{a..z}/{0..9}"
Then you can create a cronjob to clean the session folder by adding this to your crontab which deletes any session files older than an hour:
@daily find /home/askapache/tmp/s -type f -mmin +60 -exec rm -f {} \; &>/dev/null
That will create sessions in folder like:
/home/askapache/tmp/s/b/sess_b1aba5q6io4lv01bpc6t52h0ift227j6
I don't think any non-mega site will need to go more than 1 levels deep. Otherwise you create so many directories that it slows the performance gained by this.
apparently the default value for session.use_only_cookies has changed in 5.3.3 from 0 to 1. If you haven't set this in your php.ini or your code to 0 transparent sessions won't work.
Transient sessions do not appear to be working in 5.3.3
E.g.
<?php
ini_set("session.use_cookies", 0);
ini_set("session.use_trans_sid", 1);
session_start();
if (isset($_SESSION["foo"])) {
echo "Foo: " . $_SESSION["foo"];
} else {
$_SESSION["foo"] = "Bar";
echo "<a href=?" . session_name() . "=" . session_id() . ">Begin test</a>";
}
?>
This works in 5.2.5, but not 5.3.3
When setting the session.cookie_lifetime directive in a .htaccess use string format like;
php_value session.cookie_lifetime "123456"
and not
php_value session.cookie_lifetime 123456
Using a integer as stated above dit not work in my case (Apache/2.2.11 (Ubuntu) PHP/5.2.6-3ubuntu4.5 with Suhosin-Patch mod_ssl/2.2.11 OpenSSL/0.9.8g)
Recently, I needed to change the session save_path in my program under Windows. With an ini_set('session.save_path', '../data/sessions'); (and session.gc_divisor = 1 for test), I always obtain 'Error #8 session_start(): ps_files_cleanup_dir: opendir(../data/sessions) failed: Result too large'.
I corrected this by changing with ini_set('session.save_path', realpath('../data/sessions'));
In response to 00 at f00n, this very page explains:
"(...) if N is used and greater than 0 then automatic garbage collection will not be performed (...)"
So you can actually use custom save_path with automatic garbage collection, since you don't use the subdirectory option (that N subdirectory levels).
After having many problems with garbage collection not clearing my sessions I have resolved it through the following.
First I found this in the php.ini (not something i noticed as i use phpinfo(); to see my hosting ini).
; 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 would is the equivalent of
; setting session.gc_maxlifetime to 1440 (1440 seconds = 24 minutes):
; cd /path/to/sessions; find -cmin +24 | xargs rm
With this is mind there are options.
1. dont use a custom save_path.
** This means if your isp hasnt defaulted your session temp to something safer than install default or you are using a shared directory for session data then you would be wise to use named sessions to keep your session from being viewable in other people's scripts. Creating a unique_id name for this is the common method. **
2. use your custom folder but write a garbage collection script.
3. use a custom handler and a database
To get session IDs to show up in URIs, and not get stored via cookies, you must not only set session.use_cookies to 0, but also set session.use_trans_sid to 1. Otherwise, the session ID goes neither in a cookie nor in URIs!