博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
php session之多级目录存储
阅读量:4595 次
发布时间:2019-06-09

本文共 3336 字,大约阅读时间需要 11 分钟。

当选择以文件形式保存session到服务器时,需要制定保存路径。用到php.ini中的session.save_path,其有三种配置写法:

  1. session.save_path = "N;/path"
  2. session.save_path = "N;MODE;/path"
  3. session.save_path = "/path"

第3中比较常用,就不用说了。浅浅的谈下1和2。

先来看看官方手册种关于session.save_path的介绍。

session.save_path session.save_path 定义了传递给存储处理器的参数。如果选择了默认的 files 文件处理器,则此值是创建文件的路径。默认为 /tmp。参见

此指令还有一个可选的 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 bashif [[ "$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 1fiif [[ "$2" = "0" ]] && [[ ! "$4" = "recurse" ]]; then       echo "Can't create a directory tree with depth of 0, exiting."fiif [[ "$2" = "0" ]]; then       exit 0fidirectory="$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"fiif [[ "$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 - ,"fiwhile [[ -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;       donedoneif [[ ! -d $directory ]]; then       mkdir -p $directoryfiecho "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 recursedone

执行之后将创建多级目录(注意参数),自己写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

 

转载于:https://www.cnblogs.com/natian-ws/p/6652047.html

你可能感兴趣的文章
使用vue-router进行页面切换时滚动条位置与滚动监听事件
查看>>
UVA1635 Irrelevant Elements —— 唯一分解定理 + 二项式定理
查看>>
51Nod 1089 最长回文子串 V2 —— Manacher算法
查看>>
$.ajax()方法详解
查看>>
Python基础之函数二
查看>>
null和undefined区别
查看>>
Unit06 - 抽象类、接口和内部类(下) 、 面向对象汇总
查看>>
软件测试工具
查看>>
input text 的事件及方法
查看>>
(转载)Android之有效防止按钮多次重复点击的方法(必看篇)
查看>>
简单多线程拷贝单文件v2.1
查看>>
2015.5.11站立会议
查看>>
Oracle PL/SQL编程之过程
查看>>
Spring(三)--Spring bean的生命周期
查看>>
TextClock的基本使用
查看>>
.NET技术
查看>>
listview图片错位
查看>>
Python-hashlib模块
查看>>
SP348 EXPEDI - Expedition
查看>>
全栈工程师之路——服务器端自动部署
查看>>