Carl Nogry
Carl Nogry

Reputation: 65

Cancel PHP script with XHR?

I have with your help:

function send(){
  $.get("/site/send.php", function(data){
    alert(data);
  }
}

In site/send.php I have:

for($=0;$i++;$i<1000)
    sleep(1);
}
    echo "OK";

Next I have in my js file:

send();
$("#click").click(function(){
  window.stop(); // this cancel XHR
  $.get("/site/reset.php", function(data){
    alert(data);
  }
})

and in reset.php:

echo "RESET";

window.stop(); cancel my XHR but this still doesnt working OK. i cancel XHR, but still i must wait for sleep(1000) in function send(). This doesnt return anything, but still continues to execute. Only if sleep is > 1000 then /site/reset.php begins to execute.

how can i in this example cancel PHP function?

is possible check in PHP file check if XHR still isset?

Upvotes: 2

Views: 1145

Answers (3)

khael
khael

Reputation: 2610

If you have root access on the php server, if you are on linux, you can just execute some bash command, using `:

echo `ps -A`;

get you a list of all running processes on the machine, then if you know the user apache uses to execute scripts, you can then echo ps -u user;

After you identify the correct PID you can kill it with:

echo `kill PID`;

ex

echo `kill 1023`;

or

// I do not know if this works exactly this way eval('echo kill '.$_GET['PID'].';');

All of this executed in a different php script, made exactly for the purpose to kill other scripts at command.

You can use AJAX to request this kind of jobs from the script mentioned about.

Also you can make php file write to the client it's own PID (google for the write function, it looks very much like the c one) and then the javascript can send that PID directly to that killer script to command an execution stop for some script.

EDIT 1:

you can get the running script PID with this function:

getmypid();

and you can save it in a file, or better in a session variable

$_SESSION[__FILE__] = getmypid();

and to stop a file from running, you can call a page like this (killer.php):

<?php
// security verifications, etc
/*

*/ 
session_start();
$f = $_SESSION[$_GET['file']];
echo `kill $f`;

?>

It is dangerous although.

I do not know the exact commands to execute, or if you have or not that kind of control, or even if you are on a linux system. If you want I can research it a bit.

Best regards,

Upvotes: 1

Explosion Pills
Explosion Pills

Reputation: 191729

Any of the following may work:

ignore_user_abort(false);

for ($=0;$i++;$i<1000) {
   if (connection_aborted()) break;

for ($=0;$i++;$i<1000) {
   if (connection_status() != CONNECTION_NORMAL) break;

Upvotes: 2

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

$("#click").click(function(){
  //window.stop(); 
  return false;

Upvotes: 0

Related Questions