當選擇以文件形式保存session到服務器時,需要制定保存路徑。用到php.ini中的session.save_path,其有三種配置寫法:
- session.save_path = "N;/path"
- session.save_path = "N;MODE;/path"
- session.save_path = "/path"
第3中比較常用,就不用說了。淺淺的談下1和2。
先來看看官方手冊種關於session.save_path的介紹。
session.save_path
stringsession.save_path 定義了傳遞給存儲處理器的參數。如果選擇了默認的 files 文件處理器,則此值是創建文件的路徑。默認為 /tmp。參見session_save_path()。此指令還有一個可選的 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)。
Warning如果將此設定為一個全局可讀的目錄,例如 /tmp(默認值),服務器上的其他用戶有可能通過該目錄的文件列表破解會話。
Caution使用以上描述的可選目錄層級參數 N 時請注意,對於絕大多數站點,大於1或者2的值會不太合適——因為這需要創建大量的目錄:例如,值設置為 3 需要在文件系統上創建 64^3 個目錄,將浪費很多空間和 inode。
僅僅在絕對肯定站點足夠大時,才可以設置 N 大於2。
Note: 在 PHP 4.3.6 之前,Windows 用戶必須修改此選項以使用 PHP 的會話函數。必須指定一個合法路徑,例如:c:/temp。
按照手冊中的說明,先設置php.ini中的session.save_path="3;/var/lib/php/sessions"。
然后手動創建多級目錄,在php源碼中找到\ext\session\mod_files.sh
#!/usr/bin/env bash if [[ "$2" = "" ]] || [[ "$3" = "" ]]; then echo "Usage: $0 BASE_DIRECTORY DEPTH HASH_BITS" echo "BASE_DIRECTORY will be created if it doesn't exist" echo "DEPTH must be an integer number >0" echo "HASH_BITS(session.hash_bits_per_character) should be one of 4, 5, or 6" exit 1 fi if [[ "$2" = "0" ]] && [[ ! "$4" = "recurse" ]]; then echo "Can't create a directory tree with depth of 0, exiting." fi if [[ "$2" = "0" ]]; then exit 0 fi directory="$1" depth="$2" hashbits="$3" hash_chars="0 1 2 3 4 5 6 7 8 9 a b c d e f" if [[ "$hashbits" -ge "5" ]]; then hash_chars="$hash_chars g h i j k l m n o p q r s t u v" fi if [[ "$hashbits" -ge "6" ]]; then hash_chars="$hash_chars 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 X Y Z - ," fi while [[ -d $directory ]] && [[ $( ls $directory ) ]]; do echo "Directory $directory is not empty! What would you like to do?" options="\"Delete directory contents\" \"Choose another directory\" \"Quit\"" eval set $options select opt in "$@"; do if [[ $opt = "Delete directory contents" ]]; then echo "Deleting $directory contents... " rm -rf $directory/* elif [[ $opt = "Choose another directory" ]]; then echo "Which directory would you like to choose?" read directory elif [[ $opt = "Quit" ]]; then exit 0 fi break; done done if [[ ! -d $directory ]]; then mkdir -p $directory fi echo "Creating session path in $directory with a depth of $depth for session.hash_bits_per_character = $hashbits" for i in $hash_chars; do newpath="$directory/$i" mkdir $newpath || exit 1 bash $0 $newpath `expr $depth - 1` $hashbits recurse done
執行之后將創建多級目錄(注意參數),自己寫shell腳本創建也行。
創建的目錄如圖所示:
按照常規的方式使用session即可,php將按照session ID名自動匹配到相應的目錄。
session ID以hpo開頭,那么session文件將會保存到/var/lib/php/sessions/h/p/o 路徑下。
設置了多級目錄后,php將不會清除這些session文件,必須手動清除。
session.save_path = "N;MODE;/path",其中MODE為創建的session文件的權限,默認為600