drsh30452 2012-12-17 16:18
浏览 42
已采纳

php - 在代码执行之前发送HTTP状态

I want to send the http status (200 OK) to a calling script BEFORE the rest of the code executes in a php script. For sending the status code, i am using HTTP/1.1 200 OK but it is sending the status AFTER the entire script is finished executing, which makes the calling script to wait. I am not really sure if that is even possible. any help?

Detailed: say i have two scripts, a.php and b.php. a.php is calling b.php and waiting for a http status, if it is 200 then it carries on doing something else. I want the b.php script to be such that, as soon as it is called, it sends back a 200 response and then continues with its own code.

Thanks!

Edit: After reading the comments, i understood that what i wanted to do is a bad idea. However, let me say why i was thinking it and can something like that be done.

a.php would have a web interface where people can upload their images, after the upload is done a.php calls b.php which does some processing using these images. I wanted a.php to continue and not hang/wait for b.php to finish, in case b.php fails, I thought i might be able to "push" a error so that a.php can prompt the user about the failure. Should different threads be used?

  • 写回答

6条回答 默认 最新

  • dtqqq24248 2013-08-06 16:36
    关注

    PHP can certainly do it, but it's a little bit tricky.

    ignore_user_abort(true);
    header("Content-Length: 0");
    header("Connection: Close");
    flush();
    session_write_close();
    
    // do something that takes very long
    

    This is the minimal code you need. If you don't have a session you can leave off the last call.

    Once you've closed the connection you can't send updates though, so if you want to do something with error reporting you'll have to find another way.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

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