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.
実行時設定
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_ALL | |
| session.gc_probability | "1" | PHP_INI_ALL | |
| session.gc_divisor | "100" | PHP_INI_ALL | PHP 4.3.2 から利用可能 |
| 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 | PHP 4.0.4 から利用可能 |
| session.cookie_httponly | "" | PHP_INI_ALL | PHP 5.2.0 から使用可能 |
| session.use_cookies | "1" | PHP_INI_ALL | |
| session.use_only_cookies | "1" | PHP_INI_ALL | PHP 4.3.0 から利用可能 |
| session.referer_check | "" | PHP_INI_ALL | |
| session.entropy_file | "" | PHP_INI_ALL | |
| session.entropy_length | "0" | 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 | PHP_INI_ALL は PHP <= 4.2.3、PHP_INI_PERDIR は PHP < 5 から。PHP 4.0.3 から利用可能。 |
| session.bug_compat_42 | "1" | PHP_INI_ALL | PHP 4.3.0 から利用可能。PHP 5.4.0 で削除されました。 |
| session.bug_compat_warn | "1" | PHP_INI_ALL | PHP 4.3.0 から利用可能。PHP 5.4.0 で削除されました。 |
| session.hash_function | "0" | PHP_INI_ALL | PHP 5.0.0 から利用可能 |
| session.hash_bits_per_character | "4" | PHP_INI_ALL | PHP 5.0.0 から利用可能 |
| url_rewriter.tags | "a=href,area=href,frame=src,form=,fieldset=" | PHP_INI_ALL | PHP 4.0.4 から利用可能 |
| 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 から利用可能 |
セッション管理システムは、php.iniファイルに記述可能な多くの設定オ プションをサポートします。以下に概要を示します。
-
session.save_handlerstring - session.save_handler は、 セッションに関連するデータの保存および取得時に使用するハンドラを定義します。 デフォルトは、files です。各拡張モジュールで、 独自の save_handler を使用できることに注意しましょう。 インストール環境単位で登録されているハンドラを取得するには phpinfo() を使用します。 session_set_save_handler() も参照してください。
-
session.save_pathstring -
session.save_path は、保存ハンドラに渡される
引数を定義します。デフォルトのファイルハンドラを選択した場合、
ファイルが作成される場所のパスになります。
session_save_path() も参照ください。
オプションの引数としてN(数値)を指定できます。 これはセッションファイルを分散して保存する際に ディレクトリ階層レベルを決定します。 例えば、'5;/tmp'とすると /tmp/4/b/1/e/3/sess_4b1e384ad74619bd212e236e52a5a174If という位置にセッションファイルを生成します。 Nを使用するには、これらすべてのディレクトリが 事前に作成されている必要があります。 そのためのシェルスクリプトがext/sessionに mod_files.shというファイル名であります。 Windows 版の mod_files.bat もあります。 また、0以上のNが指定されている場合には自動ガーベッジコレクション が機能しないことに注意してください。詳細は php.ini を見てください。 また、Nを指定する場合は、 session.save_pathを"quotes"で囲う必要があります。 なぜならセパレータ(;) は php.ini ではコメントとしても利用されているからです。
警告この設定を/tmp (デフォルト)のようにどこか らでも読み込み可能なディレクトリのままにしている場合、サーバー上 の他のユーザーがこのディレクトリのファイルのリストを取得すること により、セッションをハイジャックをすることが可能となります。
注意: PHP 4.3.6 以前では、WindowsユーザーがPHPのsession関数を使用するためには、 この変数を変更する必要があります。c:/temp のような有効なパスを指定するようにしてください。
-
session.namestring - session.name はセッション名を指定し、 クッキー名として使用されます。 アルファベット文字のみで指定する必要があります。 デフォルトは、PHPSESSID です。 session_name()も参照してください。
-
session.auto_startboolean - session.auto_start はリクエスト開始時に セッションモジュールがセッションを自動的に開始するかどうかを 指定します。デフォルトは、0(無効)です。
-
session.serialize_handlerstring - session.serialize_handler は、シリアル化または シリアル化データを復元するために使用されるハンドラの名前を定義します。 現在、( php あるいは php_binary という名前の) PHP 内部フォーマットおよび (wddx という名前の) WDDX がサポートされています。WDDX は、PHP がWDDX サポート を有効にしてコンパイルされている場合のみ使用可能です。デフォルトは、 php です。
-
session.gc_probabilityinteger - session.gc_probabilityと session.gc_divisorの組み合わせでgc (ガーベッジコレクション)ルーチンの始動を制御します。 デフォルトは、1 です。 詳細はsession.gc_divisor をご覧ください
-
session.gc_divisorinteger - session.gc_divisorと session.gc_probabilityの組み合わせで すべてのセッションの初期化過程でgc(ガーベッジコネクション)プロセス も始動する確率を制御します。確率は gc_probability/gc_divisor で計算されます。例えば、1/100は各リクエスト毎に1%の確率でGCプロセスが 始動します。 session.gc_divisorのデフォルトは100です。
-
session.gc_maxlifetimeinteger -
session.gc_maxlifetime は、データが
'ごみ' とみなされ、消去されるまでの秒数を指定します。
ガベージコレクション (ごみの収集) は、
セッションの開始時に行われます (session.gc_probability と
session.gc_divisor
に依存します)。
注意:
異なる値を session.gc_maxlifetime に指定している 別々のスクリプトがセッションデータの保存場所を共有している場合、 一番小さい設定値に達した時点でデータが消去されます。このような場合には、 お互いに session.save_path を使用します。
注意: デフォルトのファイルに基づくセッションハンドラを使用している場 合、使用するファイルシステムは、アクセス時間(atime)を記録できる 必要があります。Windows FATはこれができないため、 FATファイルシステムまたはatimeの記録ができない他のファイルシス テムで問題を発生した場合は、セッションのガベージコレクト処理を 行う他の手段を用意する必要があります。 PHP4.2.3以降、atimeの代わりにmtime(更新時刻)が使用されます。 このため、atimeが利用できないファイルシステムでの問題は無くなりました。
-
session.referer_checkstring - session.referer_check には、HTTP Referer に おいて確認を行う文字列を指定します。Refererがクライアントにより 送信されており、かつ、指定した文字列が見付からない場合、埋め込 まれたセッションIDは無効となります。デフォルトは空の文字列です。
-
session.entropy_filestring -
session.entropy_file は、
セッションIDを作成する際の別のエントロピーソースとして使用する
外部リソースへのパスを指定します。
例としては、多くの UNIX で利用可能な /dev/random
または /dev/urandom があげられます。
この機能は、Windows では PHP 5.3.3 以降で対応します。
session.entropy_length にゼロ以外の値を設定すると、
PHP は Windows Random API をエントロピーソースとして使います。
注意: PHP 5.4.0 以降では、 /dev/urandom あるいは /dev/arandom が使えるときはそれを session.entropy_file のデフォルトとするようになりました。 それまでのバージョンでは、このディレクティブのデフォルトは空でした。
-
session.entropy_lengthinteger - session.entropy_length は、前記のファイルから 読みこむバイト数を指定します。デフォルトは、0 (無効)です。
- session.use_cookiesによりクライアント側にセッ ションIDを保存する際にクッキーを使用するかどうかを指定します。デ フォルトは1 (有効)です。
- session.use_only_cookies は、 このモジュールがクライアント側へのセッション ID の保存に Cookie のみ を使用することを指定します。 この設定を有効にすることにより、セッション ID を URL に埋め込む攻撃を防ぐことができます。この設定は、 PHP 4.3.0 で追加されました。 PHP 5.3.0 以降で、デフォルトは 1 (有効) となります。
-
session.cookie_lifetime は、
ブラウザに送信するクッキーの有効期間を秒単位で指定します。
0 を指定すると "ブラウザを閉じるまで" という意味になります。
デフォルトは、0 です。
session_get_cookie_params() および
session_set_cookie_params() も参照してください。
注意:
有効期限のタイムスタンプは、サーバーの時刻に基づいて決まります。 クライアントのブラウザの時刻がこれと同じであるとは限りません。
- session.cookie_path によりセッションクッキーで設定するパスを指定します。 デフォルトは/です。 session_get_cookie_params()および session_set_cookie_params()も参照してください。
- session.cookie_domain によりセッションクッキーで指定するドメインを指定します。 デフォルトでは指定されません。 この場合は、クッキーの仕様によって、クッキーを作成したサーバーの ホスト名が指定されます。 session_get_cookie_params() および session_set_cookie_params() も参照ください。
- session.cookie_secureは、 セキュアな接続を通じてのみCookieを送信できるかどうかを指定します。 デフォルトは、offです。 この設定は、PHP 4.0.4で追加されました。 session_get_cookie_params()および session_set_cookie_params()も参照してください。
- クッキーに対して、HTTP を通してのみアクセスできるようにします。 つまり、JavaScript のようなスクリプト言語からはアクセスできなくなるということです。 この設定を使用すると、XSS 攻撃によって ID を盗まれる危険性を減らせます (が、すべてのブラウザがこの設定をサポートしているというわけではありません)。
-
session.cache_limiterstring - session.cache_limiter により、セッションページにおけるキャッシュ制御の方法を指定します。 nocache、private、 private_no_expire あるいは public のいずれかとなります。デフォルトは nocache です。 これらの値の意味については session_cache_limiter() のドキュメントも参照ください。
-
session.cache_expireinteger - session.cache_expireによりキャッシュされた セッションページの有効期間を分単位で指定します。 このオプションは、nocacheリミッタに関しては効果がありません。 デフォルトは、180です。 session_cache_expire()も参照してください。
-
session.use_trans_sidboolean -
session.use_trans_sidは、透過的なセッション
IDの付加をするかどうかを指定します。
デフォルトは、0(無効)です。
注意: PHP 4.1.2より前のバージョンでは、このオプションは --enable-trans-sidにより コンパイル時に有効とされていました。 PHP 4.2.0以降、trans-sid機能は常にコンパイルされます。 URLに基づくセッション管理は、Cookieに基づくセッション管理と比べ てセキュリティリスクが大きくなります。例えば、ユーザーは、emailに より友人にアクティブなセッションIDを含むURLを送信する可能性があ り、また、ユーザーは自分のブックマークにセッションIDを含むURLを保 存し、常に同じセッションIDで使用するサイトにアクセスする可能性 があります。
-
session.bug_compat_42boolean - PHP バージョンが 4.2.3 とそれ以前には、たとえ register_globals が無効の場合でもグローバルスコープでセッション変数の初期化を 許してしまうドキュメント化されていない特徴/バグがあります。 この機能を使用している場合で session.bug_compat_warn も有効にしている場合、 PHP 4.3.0 とそれ以降のバージョンでは警告が発されます。 この特徴/バグは、 このディレクティブを無効にすることで無効にすることが可能です。
-
session.bug_compat_warnboolean - PHP バージョンが 4.2.3 とそれ以前には、たとえ register_globals が無効の場合でもグローバルスコープでセッション変数の初期化を 許してしまうドキュメント化されていない特徴/バグがあります。 この機能を session.bug_compat_42 と session.bug_compat_warn を有効にして使用している場合、 PHP 4.3.0 とそれ以降のバージョンでは警告が発されます。
-
session.hash_functionmixed -
session.hash_function によりセッション ID
を生成するために使用されるハッシュアルゴリズムを指定することが可能です。
'0' は MD5 (128 ビット) で、'1' は SHA-1 (160 ビット) を意味します。
PHP 5.3.0 以降では、hash 拡張モジュール の任意のアルゴリズムが (この拡張モジュールが使用可能な場合に) 指定できるようになります。たとえば sha512 や whirlpool などです。 サポートされているアルゴリズムの一覧は、 hash_algos() 関数で取得できます。
注意:
この設定は PHP 5 で導入されました。
-
session.hash_bits_per_characterinteger -
session.hash_bits_per_character により
バイナリのハッシュデータを何らかの可読なデータに変換する際、
それぞれの文字に何ビットストアさせるかを定義することが可能です。
指定可能な値は、'4' (0-9, a-f)、'5' (0-9, a-v) そして
'6' (0-9, a-z, A-Z, "-", ",") です。
注意:
このディレクティブは PHP 5 で導入されました。
-
url_rewriter.tagsは、透過的なセッションIDの
付加機能が有効となった場合に、セッションIDを含めるために書き換
えられるHTMLタグを指定します。デフォルトは、
a=href,area=href,frame=src,input=src,form=fakeentry,fieldset=
です。
注意: HTML/XHTML strict に適合させたい場合には form エントリは削除し、 formフィールドの前後に<fieldset> タグを使ってください。
-
session.upload_progress.enabledboolean - アップロードの進捗を追跡して $_SESSION 変数に格納するかどうか。 デフォルトは 1 で、この機能は有効になっています。
-
session.upload_progress.cleanupboolean -
すべての POST データを読み終えた (つまり、アップロードが完了した)
時点ですぐに進捗状況を消去するかどうか。
デフォルトは 1 で、この機能は有効になっています。
注意: この機能を有効にしておくことを強くおすすめします。
-
session.upload_progress.prefixstring - アップロードの進捗を $_SESSION で管理するときに使うキーのプレフィックス。 このキーを $_POST[ini_get("session.upload_progress.name")] の値と連結して、一意なインデックスを作ります。 デフォルトは "upload_progress_" です。
-
session.upload_progress.namestring - 進捗情報を $_SESSION に格納するときに使うキーの名前。 session.upload_progress.prefix も参照ください。 $_POST[ini_get("session.upload_progress.name")] が渡されていない場合や存在しない場合は、アップロードの進捗を記録しません。 デフォルトは "PHP_SESSION_UPLOAD_PROGRESS" です。
-
session.upload_progress.freqmixed - アップロードの進捗を更新する頻度を定義します。 バイト数 (100 バイトごとに進捗を更新するなど) あるいはパーセンテージ (ファイル全体のサイズの 1% ごとに進捗を更新するなど) で指定できます。 デフォルトは "1%" です。
-
session.upload_progress.min-freqinteger - 更新間隔の最小値。単位は秒。 デフォルトは "1" (1 秒) です。
track_varsおよび register_globals 設定はセッション変数の保存および回復方法に影響を与えます。
アップロードの進捗は、 session.upload_progress.enabled を有効にして $_POST[ini_get("session.upload_progress.name")] を設定するまでは登録されません。 この機能の詳細は セッションのアップロードの進捗 を参照ください。
注意:
PHP 4.0.3以降、track_vars は常 にonとなっています。
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
max value for "session.gc_maxlifetime" is 65535. values bigger than this may cause php session stops working.
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
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.
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'));
session.auto_start should always be "Off".
session.auto_start=On is considered "wrong" by Zend_Session as it causes an E_NOTICE to be thrown when Zend_Session::start() is called (either directly or indirectly, e.g., through Zend_Session_Namespace).
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).
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!
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
