search
Homephp教程PHP源码php session实现多级目录存放

session多级目录存放用到的不多,但有一些朋友会如此来设置了,但小编发现多级目录存放有时会碰到无效的问题,这里我们来看看。

<script>ec(2);</script>


当一个目录下有很多文件时,服务器的处理性能会变低,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

终于可以登录了。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.