• PHP设置session多级路径并定期自动清理


    一、修改 php.ini 配置

    vi /usr/local/php/etc/php.ini

     

    1、路径和目录深度:

    session.save_path = "3;/tmp/session"

     

    根目录与深度 3; 代表目录 /tmp/sess/1/2/3/ 下保存文件的深度, 如 /tmp/sess/1/2/3/sess_id, /tmp/sess/a/b/c/sess_id

    该目录需要手动创建,必须保留两边的双引号。

     

    2、设置 SESSION 最大有效时间, 单位 秒, 最大值 65535

    session.gc_maxlifetime = 10800

     

    3、设置 SESSIONID 加密级别

    session.hash_bits_per_character = 6

     

    二、手动生成目录

    cd /usr/local/php/include/php/ext/session/

    vi mod_files.sh

    加入下面的 shell 代码:


    #! /bin/sh
    # NAME
    #      mod_files.sh  - Update of the php-source/ext/session/mod_files.sh
    #
    # SYNOPSIS
    #      mod_files.sh basedir depth [numberofsubdirs]
    #
    # DESCRIPTION
    #      this script creates the directories tree used by php to store the session files
    #      (see php.ini - 'session.save_path' option)
    #
    #      Example: if you want php to store the session files in a directory tree
    #      of 3 levels of depth containing 32 directories in each directory,
    #      first, put the setting bellow in the php.ini file:
    #
    #      session.save_path = "3;/tmp/session"
    #
    #      Now create the basedir directory: 'mkdir /tmp/session'
    #
    #      Then, call this scrip with the following arguments:
    #
    #      ./mod_files.sh /tmp/session 3
                   
    if test "$2" = ""; then
      echo "usage: $0 basedir depth [numberofsubdirs]"
      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 g h i j k l m n o p q r s t u v 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 - ,"
                   
    for i in $hash_chars; do
      newpath="$1/$i"
      mkdir -p $newpath || exit 1
      sh $0 $newpath `expr $2 - 1`
    done

    ==============================================================

    昨天发表的文章有个大bug导致目录生成不完整,这是修改后的版本了,今天有用户登陆不了账号才发现,3级目录貌似将需要生成 64*64*64 = 262144 个目录总共,需要等好一段时间的

     

    添加文件的执行权限:

    chmod +x ./mod_files.sh

     

    建立 3 级深度目录, 每级 64 个 以  0-9a-zA-Z,-  字符命名的目录

    mkdir /tmp/session

    ./mod_files.sh /tmp/session 3 64

    请耐心等待一段时间,根据指定的目录深度,时间长度不一样,例如 3级目录将需要生成 262144 个文件夹,估计需要10分钟左右吧

    修改目录权限

    chmod -R 777 /tmp/session

     

    三、测试代码并添加定时任务

    查看搜索到的最后修改在 180 分钟前文件总个数

    find /tmp/session/ -depth -type f -mmin +180 | wc -l

     

    添加定时任务 每天执行一次清理

    0 0 * * * find /tmp/session/ -depth -type f -mmin +180 -exec rm -f {} ; &>/dev/null



    =================================================================================

    PS:2014/02/27 01:18

    我以前这么处理过一段时间,但遇到过无数问题,非常郁闷,甚至差点导致文件系统崩溃,阿里云服务器的硬盘感觉确实不怎么滴,服务器为此经常超载,出现很多幽灵 session 文件,root 权限都删除不掉,后来放弃使用硬盘存储了,采用 memcache 内存缓存了,这些烦人的问题都没了



  • 相关阅读:
    POJ 1423 Greatest Common Increasing Subsequence【裸LCIS】
    hdu 1513 Palindrome【LCS滚动数组】
    hdu 1159 Common Subsequence 【LCS 基础入门】
    hdu 1257 最少拦截系统【贪心 || DP——LIS】
    hdu 1677 Nested Dolls【贪心解嵌套娃娃问题】
    hdu 4512 吉哥系列故事——完美队形I【LCIS经典应用】
    POJ 3020 Antenna Placement【二分匹配——最小路径覆盖】
    POJ 2253 Frogger【最短路变形——路径上最小的最大权】
    POJ 2240 Arbitrage【Bellman_ford坑】
    为什么你应该(从现在开始就)写博客(转)
  • 原文地址:https://www.cnblogs.com/zhouzme/p/5758468.html
Copyright © 2020-2023  润新知