douwu8524 2017-05-08 13:34
浏览 68

异步脚本执行PHP

I need to execute a PHP script without blocking user interface.

Look this (light) example :

<?php
   echo "user see this";
   header("Location:otherpage.php");

        sleep(10);
        for($i=1; $i < 100000; $i++){
            $t = fopen("test.txt", "a+");
            fwrite($t, "$i
");
            fclose($t);
        }
?>

This example need too much time to been executed, how to make it asynchronous and redirect the user witout interface waiting ???

  • 写回答

1条回答

  • dpp3047 2017-05-08 14:15
    关注

    I found a script who made what I need, but I don't understand it :

        ignore_user_abort(true);
    set_time_limit(0);
    
    $strURL = "PUT YOUR REDIRCT HERE";
    header("Location: $strURL", true);
    header("Connection: close", true);
    header("Content-Encoding: none
    ");
    header("Content-Length: 0", true);
    
    flush();
    ob_flush();
    
    session_write_close();
    
    // Continue processing...
    
    sleep(100);
    exit;
    

    Someone can explain ?

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器