LoginSignup
37
37

More than 5 years have passed since last update.

phpでcronもatも使わず簡単background処理

Posted at

画面表示だけ先にして、実際の処理はあとでゆっくり実行する。
cronもatも使わずに。な方法です。

下記そのままです。
http://php.net/manual/en/features.connection-handling.php
Content-Encoding: gzip だとうまくいかないみたいです。

php

function init_background_process(){
    ob_end_clean();
    header("Connection: close\r\n");
    header("Content-Encoding: none\r\n");
    ignore_user_abort(true);
    ob_start();
}

function start_background_process(){
    $size = ob_get_length();
    header("Content-Length: $size");
    ob_end_flush();
    flush();
    ob_end_clean();
}

//画面表示の前に実行
init_background_process();

echo 'メール送信を受け付けました';

//バックグラウンド処理の直前に実行
start_background_process();

sleep(5);

for($i = 1; $i < 100; $i++){
  mb_send_mail('hoge@fuga.com', 'test', 'test', 'From: fuga@hoge.com');
}




37
37
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
37
37