美文网首页PHP开发札记
PHP ignore_user_abort 使用

PHP ignore_user_abort 使用

作者: 小布走慢点 | 来源:发表于2016-06-06 14:40 被阅读377次

结合set_time_limit()函数实现一个循环脚本执行任务

<?php
ignore_user_abort();
set_time_limit(0);
$interval=60*15;
do{
  if(xxx)break;//记住退出
  //执行的业务
}while(true);
?>//说明:每隔15分钟循环执行

自定义实现文件输出并跟踪ignore_user_abort()函数的执行结果

<?php 
ignore_user_abort ( TRUE );
set_time_limit ( 0 );
$interval = 10;
$stop = 1;
do {    
if( $stop == 10 ) break;    //退出
file_put_contents('liuhui.php',' Current Time: '.time().' Stop: '.$stop);//记得权限
$stop++;  
sleep ( $interval );
} while( true);
?>

Demo

用于某些免费的 php 虚拟机很不错
[代码]task.php(任务主文件)

<?php
ignore_user_abort(); //关掉浏览器,PHP脚本也可以继续执行.
set_time_limit(0); // 通过set_time_limit(0)可以让程序无限制的执行下去
$interval = 15; // 每隔*秒运行
$temp_key = 0;
do {
    $time = time();
    require 'config.php';
    $mem = new Memcache();
    $mem->connect($Memcache_server, $Memcache_port);
    if ($is_send) {
        $get_time = $mem->get('tem_data');
    } else {
        $get_time = $time + 86400;
        exit();
    }
    if ($get_time == $time) {
        $mem->close();
        exit();
    } else if ($get_time > $time - $interval) {
        $mem->close();
    } else if ($temp_key == 0) {
        $temp_key = 1;
        @file_get_contents('http://******/*****.php');
        $mem->set('tem_data', $time, MEMCACHE_COMPRESSED, $Memcache_date);
        $mem->close();
        sleep(mt_rand(1, 3));
        $temp_key = 0;
    }else{
        $mem->close();
        exit();
    }
    //这里是你要执行的代码
    sleep($interval); // 等待*秒钟
} while (true);
?>

[代码]config.php(配置文件)

<?php
$Memcache_server = '127.0.0.1';
$Memcache_port = 11211;
$Memcache_date = 86400;
//return $is_send = false; //关闭定时任务
return $is_send = true;  //开启定时任务
?>

参考

PHP自动定时循环执行任务
PHP实现执行定时任务的几种思路详解
PHP 函数 ignore_user_abort定时执行任务的实现

相关文章

  • PHP ignore_user_abort 使用

    结合set_time_limit()函数实现一个循环脚本执行任务 自定义实现文件输出并跟踪ignore_user_...

  • php中ignore_user_abort函数的用法 php脚本

    ignore_user_abort() 可以实现当客户端关闭后仍然可以执行PHP代码,可保持PHP进程一直在执行,...

  • PHP定时执行脚本

    ignore_user_abort(); //关闭浏览器脚本也可执行 set_time_limit(0); // ...

  • php - 杂项函数

    ignore_user_abort() //函数设置与客户机断开是否会终止脚本的执行。

  • MySQL批量处理

    一、使用PHP exec 启动 php 服务,出现 [php] 程序中,禁止使用此函数二、 大...

  • PHP 疑难杂症

    PHP 使用unsigned bigint PHP 使用unsigned bigint作为主键时应该使用PHPx6...

  • 2018-09-12

    php优化的方法 1、服务器使用Linux系统 2、使用Nginx或Apache来运行PHP 3、开始使用PHP7...

  • 认识php

    认识php 在网页中使用php输出helloword PHP 页面 ...

  • mac php xdebug3

    重启php brew services restart php php 查看相关信息 使用 php -i 查看ph...

  • PHP 类自动加载机制

    版本 PHP5.1.2 之前使用 __autoload() 。PHP5.1.2 之后使用 spl_autoload...

网友评论

    本文标题:PHP ignore_user_abort 使用

    本文链接:https://www.haomeiwen.com/subject/srzrdttx.html