首页 > 编程技术 > php

PHP设置session定期自动清理的例子

发布时间:2016-11-25 15:36

下文来为各位介绍PHP设置session定期自动清理的例子了,因为session默认是15分钟自动把变量给清除内存了,但有一些时间并不生效了,下面我们就来看看.

配置完成php后,默认php会将session生成到/tmp目录下,导致/tmp目录文件很多,因此需要对session进行定期的清理:

修改php.ini:

[root@hz scripts]# grep "session.save_path = " /usr/local/php/lib/php.ini
;     session.save_path = "N;/path"
;     session.save_path = "N;MODE;/path"
;session.save_path = "/tmp"
session.save_path = "2;/tmp/session"
[root@hz ~]# cat /byrd/script/Cleartmpsen.sh
#!/bin/bash
# Version:1.0
# Author:Byrd
# Site:www.t4x.org
# Contact:root#t4x.org

# This is script will clear php session before 3 hours.
i="0 1 2 3 4 5 6 7 8 9 a b c d e f"
for byrd in $i;
do
    for x in $i;
    do
        mkdir -p /tmp/session/$byrd/$x;
    done;
done
chown -R bywww:bywww /tmp/session
chmod -R 1777 /tmp/session
find /tmp/session -amin +180 -exec rm -rf {} \;
if [ `grep 'session.save_path =' /usr/local/php/lib/php.ini | wc -l` -eq 3 ];then
    sed -i 's#;session.save_path = "/tmp"#;session.save_path = "/tmp"\nsession.save_path = "2;/tmp/session"#g' /usr/local/php/lib/php.ini
else
    exit 1
fi

定时任务:

[root@hz scripts]# echo '#This is a config php session BY:BYRD AT:2014-03-31' >>/var/spool/cron/root
[root@hz scripts]# echo '0 3 * * 0 /bin/bash /byrd/scripts/spehspsion.sh >/dev/null 2>&1' >>/var/spool/cron/root


备注:因为此配置修改了php.ini因此需要重新启动php进程。

Laravel是一套简洁、优雅的PHP Web开发框架(PHP Web Framework)。它可以让你从面条一样杂乱的代码中解脱出来;它可以帮你构建一个完美的网络APP了,下面我们来看看laravel excel包使用教程

laravel中excel插件的安装

在composer中引入laravel excel的包

    "maatwebsite/excel": "1.*"
在位于laravel/app/config下编辑app.php文件,在providers数组中添加以下值

    'Maatwebsite\Excel\ExcelServiceProvider',
在同文件中找到aliasses数组添加以下值

    'Excel' => 'Maatwebsite\Excel\Facades\Excel',
执行composer install 或 composer update命令.

laravel excel的配置

在位于laravel/vendor/maatwebsite/excel/src/config下一些对于插件的一些配置项

config.php > 对excel和表全局的一些设置
csv.php > 对导入导出csv文件的设置
export.pho > 对打印出文件内容的一些设置
import.php > 对导入excel文件的设置

laravel excel的简单使用

在之前的准备工作都做好了以后我们就可以用excel插件了

导出excel

<?php
$rows = array( array( 'id' => 1, 'name' => 'marlon' ) );

Excel::create($name, function($excel) use ($rows) {
    $excel->sheet('当天报名', function($sheet) use ($rows) {
        $sheet->fromArray($rows);
    });
})->store('xls', storage_path('excel'));

由于在php闭包中无法拿到闭包外的变量,所以需要用use把$rows引入进去,在最后的链式调用的store中所传的参数就是所需excel的格式和要保存到服务器的位置,此为绝对路径.

在这个地方store()方法为存储,相对应的还可以使用download()方法来直接下载,至于export方法笔者还没搞懂用处是什么

导入excel

<?php
Excel::load(Input::file('excel'), function($reader) {
    //获取excel的第几张表
    $reader = $reader->getSheet(0);
    //获取表中的数据
    $results = $reader->toArray();
    //在这里的时候$results 已经是excel中的数据了,可以再这里对他进行操作,入库或者其他....
});

301跳转必须由程序或服务器来实现,如果是页面跳转可以使用js或页面html来实现,下面我们先来介绍页面跳转,然后再介绍301跳转了.


一般情况下,此类跳转是302跳转,只是暂时性跳转,如果需要进行永久重写向(SEO上比较有用),可如下实现:

header("HTTP/1.1 301 Moved Permanently");
header("Location: redirect.php");


平时我们使用html做页面的时候,都会碰到定时刷新,可以看到如下标签:

<meta http-equiv="Refresh" content="2;url='refresh.php'">

表示每隔两秒刷新一次页面,实际上是重定向到页面。

由此,PHP根据HTTP协议,可以如下实现:

header( "refresh:2;url=refresh.php" );

1.window.location.href方式
    <script language="javascript" type="text/javascript">
           window.location.href="target.aspx";
    </script>

2.window.navigate方式跳转
   <script language="javascript">
    window.navigate("target.aspx");
</script>

 

3.window.loction.replace方式实现页面跳转,注意跟第一种方式的区别
<script language="javascript">
    window.location.replace("target.aspx");
</script>
有3个jsp页面(1.aspx, 2.aspx, 3.aspx),进系统默认的是1.aspx,当我进入2.aspx的时候, 2.aspx里面用window.location.replace("3.aspx");

与用window.location.href ("3.aspx");

从用户界面来看是没有什么区别的,但是当3.aspx页面有一个"返回"按钮,调用window.history.go(-1); wondow.history.back();方法的时候,一点这个返回按钮就要返回2.aspx页面的话,区别就出来了,当用 window.location.replace("3.aspx");连到3.aspx页面的话,3.aspx页面中的调用 window.history.go(-1);wondow.history.back();方法是不好用的,会返回到1.aspx。

4.self.location方式实现页面跳转,和下面的top.location有小小区别
   <script language="JavaScript">
          self.location='target.aspx';
   </script>

5.top.location
   <script language="javascript">
          top.location='target.aspx';
   </script>

网站有从远程服务器通过http下载文件的需求,于是写了个提供文件下载的脚本,下面我们来看看用PHP如何实现文件下载功能的。

偶尔用用PHP写点这种很小很小的Web程序,还是蛮简单方便的。

PHP实现文件下载功能的代码如下

<?php
$base_dir = "/usr/share/nginx/html/";
$myfile = $base_dir . $_GET["file"];
//echo $myfile;
 
if( ! file_exists($myfile) ) {
echo "file: " . $myfile . " doesn't exist.";
} elseif ( is_dir($myfile) ) {
echo "file: " . $myfile . " is a directory.";
} else {
    header("Content-type: application/octet-stream");
    header('Content-Disposition: attachment; filename="' . basename($myfile) . '"');
    header("Content-Length: ". filesize($myfile));
    readfile($myfile);
}
?>

github: https://github.com/smilejay/other-code/blob/master/php/download.php

另外,一个牛人分析一下使用apache/nginx的一些模块让php实现问下下载更加的高效:

http://www.laruence.com/2012/05/02/2613.html

说到缓存估计大家会想到ob_start函数吧,但如果要做到嵌套输出缓存的话我们是需要使用另一个函数了,下面一起来看看吧。

PHP的输出缓存是可以嵌套的。用ob_get_level()就可以输出嵌套级别。

测试发现在cli和浏览器下输出结果不一样(PHP5.4)。

ob_level1.png手册说明如下:

ob_get_level() will always return 0 inside a destructor.
This happens because the garbage collection for output buffers has already done before the destructor is called

想要正确输出也很简单:

ob_end_clean();
echo ob_get_level(); //0

回到正题:

ob_end_clean();
 
ob_start();
echo 'php1';//此处并不会在页面中输出
$a = ob_get_level();
$b = ob_get_contents();//获得缓存结果,赋予变量
ob_clean();
 
ob_start();
echo 'php2';//此处并不会在页面中输出
$c = ob_get_level();
$d = ob_get_contents();//获得缓存结果,赋予变量
ob_clean();
 
ob_start();
echo 'php3';//此处并不会在页面中输出
$e = ob_get_level();
$f = ob_get_contents();//获得缓存结果,赋予变量
ob_clean();
 
echo 'level:'.$a.',ouput:'.$b.'<br>';
echo 'level:'.$c.',ouput:'.$d.'<br>';
echo 'level:'.$e.',ouput:'.$f.'<br>';

结果如下:

level:1,ouput:php1
level:2,ouput:php2
level:3,ouput:php3

当然,当你关闭某个级别的缓冲,如下测试:
ob_end_clean();
 
ob_start();
echo 'php1';
$a = ob_get_level();
$b = ob_get_contents();
ob_clean();
 
ob_start();
echo 'php2';
$c = ob_get_level();
$d = ob_get_contents();
ob_end_clean();  //清空缓存并关闭缓存
 
ob_start();
echo 'php3';
$e = ob_get_level();
$f = ob_get_contents();
ob_clean();
 
echo 'level:'.$a.',ouput:'.$b.'<br>';
echo 'level:'.$c.',ouput:'.$d.'<br>';
echo 'level:'.$e.',ouput:'.$f.'<br>';

结果如下:

level:1,ouput:php1
level:2,ouput:php2
level:2,ouput:php3

标签:[!--infotagslink--]

您可能感兴趣的文章: