zoukankan      html  css  js  c++  java
  • php session之多级目录存储

    当选择以文件形式保存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 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源码中找到extsessionmod_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

  • 相关阅读:
    资源-python 视频下载大全
    ubuntu 16.04(操作应用) -软件卸载
    资源-简历的相关知识
    centos (命令操作)-crontab命令
    ubuntu 16.04 (软件应用)-输入法
    ntp时间同步
    lvm空间扩容
    目录知识
    Linux下安装maven
    elasticsearch安装pinyin模块
  • 原文地址:https://www.cnblogs.com/natian-ws/p/6652047.html
Copyright © 2011-2022 走看看