0

I found some core php code from Google. I tested this code is working fine. But While I am using this code in my Yii application this code is not working. Please check my code below:

/* filename: test.php */
date_default_timezone_set('Asia/Calcutta');
ini_set("error_log", "error.log");

echo "We'll email you as soon as this is done.";

$size = ob_get_length();

header("Content-Length: $size");
header('Connection: close');

// flush all output
ob_end_flush();
//ob_flush();
flush();
sleep(30);
/* notification and other code here */
error_log("Background process end");

When I am testing above code in separate file like test.php this is working fine. But Not working in Yii controller action.

I have set sleep 30 seconds of my background process execution time. 1st time while I hit my controller action in browser this is working and Page reload couple of microseconds after. While again I refresh this page or any other pages That pages reloading until my 1st execution not done.

Ref:

http://php.net/manual/en/features.connection-handling.php

close a connection early

How to fix it?

Chinmay235
  • 3,236
  • 8
  • 62
  • 93

1 Answers1

0

As a work around try

$cmd = "php somescript.php $"
shell_exec( $cmd )

Or implement a Multithread as shown here! How can one use multi threading in PHP applications

Cyberience
  • 972
  • 10
  • 15
  • My PHP version 7.1. I am unable to install multithread. Could you suggest me any dll file. I have used Windows7 – Chinmay235 Nov 24 '17 at 11:06