php session實現多級目錄存放

來源:互聯網
上載者:User


當一個目錄下有很多檔案時,伺服器的處理效能會變低,php預設的session僅僅存放在/tmp目錄下,未進行分級,當有一定的訪問量時,就存在效能問題了。

首先,修改 php.ini的 session.save_path 選項修改如下:
session.save_path = “2;/tmp/session” (去掉前面分號)
表示把session存放在 “/tmp/session” 目錄下,並且分成 2 級子目錄

一般情況下2級目錄就夠了,就能夠處理相當大的訪問量了

———————–

其他注釋

session.hash_function = 0
; 產生SID的散列演算法。SHA-1的安全性更高一些
; 0: MD5 (128 bits)
; 1: SHA-1 (160 bits)
; 建議使用SHA-1。

session.hash_bits_per_character = 4
; 指定在SID字串中的每個字元內儲存多少bit,
; 這些位元是hash函數的運算結果。
; 4: 0-9, a-f
; 5: 0-9, a-v
; 6: 0-9, a-z, A-Z, “-“, “,”
; 建議值為 5

————————–

php源碼檔案中ext/session/mod_files.sh檔案,可以輔助組建目錄,就不用自己在寫指令碼了


#! /bin/sh
 
if test "$2" = ""; then
echo "usage: $0 basedir depth"
exit 1
fi
 
if test "$2" = "0"; then
exit 0
fi
 
hash_chars="0 1 2 3 4 5 6 7 8 9 a b c d e f"
if test "$3" -a "$3" -ge "5"; then
hash_chars="$hash_chars g h i j k l m n o p q r s t u v"
if test "$3" -eq "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
fi
 
for i in $hash_chars; do
newpath="$1/$i"
mkdir $newpath || exit 1
sh $0 $newpath `expr $2 - 1` $3
done
設定為可執行之後,運行以下命令來建立雜湊目錄:
#cd /root/soft_install/php-5.3.5/ext/session
#./mod_files.sh /tmp/session 2 5

三個參數依次表示,存放路徑, 幾級目錄,每個目錄產生多少個目錄(參考session.hash_bits_per_character)

另外需要注意修改 /tmp/session的許可權,保證運行php的帳號有許可權讀寫

php中設定多級目錄session的問題


在 php.ini 中找到 session.save_path 將值設定為 session.save_path = '3;/tmp/session'; 即可開啟三級目錄儲存session。但是php不會自動組建目錄結構,這時可以藉助源碼包 ext/session 目錄下的 mod_files.sh 來組建目錄

$ bash mod_files.sh /tmp/session 3

產生完成後發現仍然不能產生session,糾結了半天,開啟mod_files.bat才發現玄機,原來後面還需要帶一個參數,對應於 php.ini 中的 session.hash_bits_per_character ,這個值預設是4,development和production版本的預設配置裡是5,於是用下面的命令從新組建目錄

$ bash mod_files.sh /tmp/session 3 5

終於可以登入了。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.