ajax - php response is not sent before the script ends -


i issue ajax post method send commands php backend. 1 one these "reboot". i'm trying answer before script ends:

<?php  set_time_limit(0);  $json = file_get_contents('php://input'); $obj = json_decode($json);  switch ($obj->{'action'}) {   case "reboot":     $data = '{"success" : true}';      ob_start();     header('content-type: application/json');     echo json_encode($data);     ob_end_flush();     ob_flush();     flush();      exec("sudo reboot");     break;    default:     $data = '{"success": "fail"}';     break; } 

the ajax call (on client side) times out , never receives answer. of course can add small delay reboot command, want understand why code not working.

i'm running on rpi lighttpd , fast-cgi.

you can define cron job run on every second , check setting, reboot or of like. if that's true, reboot issued. script issued ajax call set setting true script can respond client-side. when server rebooted, reboot setting automatically set false. seems extremely unsafe approach, since user might able send such request, rendering server unusable.


Comments