2

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?

9
  • 1
    You can't cancel a running php script unless it periodically checks for an exit conditions. Cancelling the XHR request does nothing. Commented Dec 12, 2011 at 11:40
  • so how can i make this correctly? Commented Dec 12, 2011 at 11:42
  • and why would you even try to do that? Explain what you want to do and maybe you get a way better solution do achieve it. Commented Dec 12, 2011 at 11:42
  • 1
    1) make the php script check a condition (e.g. content of an helper file), if 1, abort, else continue running. 2) on button click send an ajax request to some other php-handler, which will set the contents of the helper file to 1. Commented Dec 12, 2011 at 11:47
  • but in this example still one script PHP is running and i can't execute new script Commented Dec 12, 2011 at 12:12

3 Answers 3

2

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;
Sign up to request clarification or add additional context in comments.

Comments

1

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,

Comments

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

2 Comments

"how can i in this example cancel PHP function?"
I doubt this has anything to do with what the user is asking. This simply stops the click event from propogating.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.