Re: Cancel a query when user leaves a web page

From: Noel <noel(dot)faux(at)med(dot)monash(dot)edu(dot)au>
To: "Muhyiddin A(dot)M Hayat" <middink(at)indo(dot)net(dot)id>, pgsql-php(at)postgresql(dot)org, ljb220(at)mindspring(dot)com, Di Wu <ever_wudi(at)yahoo(dot)com>
Subject: Re: Cancel a query when user leaves a web page
Date: 2003-12-16 07:58:17
Message-ID: 3FDEBB19.9050100@med.monash.edu.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-php

Hi all,
Sorry for the delayed response, I've added both Hayat's and ljb220's
suggestions.

I've tried both, but still having problems.
Code:
//----------------------
test.php
<html>
<script language="JavaScript">
<!-- Begin
function leave() {

window.open('close.php','','toolbar=no,menubar=no,location=no,height=10,width=10','');
}
// end -->
</script>
<body onunload = 'leave()'>
<?php include "functions.php";
global $connection;
$connection = pg_connect("host=localhost dbname=test user=test");
echo $connection." connection <br>";
echo pg_connection_status($connection)." status<br>";
pg_send_query($connection, "SELECT COUNT(id) FROM test"); //
expected to take ~2min enough time to press stop
echo pg_connection_busy($connection)." busy<br>";
$res = pg_get_result($connection);
echo pg_num_rows($res)." # rows<br>";
echo $GLOBALS["connection"]." GLOBALS<br>";
?>
</body></html>

//--------------------------------------
close.php
<?php include "functions.php";
echo "closing connection<br>";
$f = fopen("/home/xxx/log","w"); // log file to trace the code etc...
fwrite($f,"new page\n");
fclose($f);
close_con();
echo "closed";
echo "<script>window.close();</script>";
?>

//--------------------------------
functions.php
<?php
/* when the page is exited, if the connection is not null, cancel and
sql statements and
close the database connection */
function close_con() {
$f = fopen("/home/xxx/log","w"); // log file to trace the code
etc...
$connection = $GLOBALS["connection"];
fwrite($f,"Closing connection\n");
$status = pg_connection_status($connection);
fwrite($f, $status." status\n");
if($status == 0){// connected
fwrite($f, "connected\n");
if(pg_connection_busy($connection)) {// busy so cancel last
query
fwrite($f, "connection busy\n");
pg_cancel_query($connection);
fwrite($f, "query killed\n");
}
// close connection
pg_close($connection);
fwrite($f, "connection closed");
}
else {
fwrite($f, "not connected\n");
}
fclose($f);
}
?>

With this set up, the user enters test.php. Stops before the query
finishes. It should call leave(). However, this dose not happen. I
believe it is because the script has not finished and passed the page
text to the browser (pls correct me if I'm wrong), until php has
finished processing it.

I've tried using register_shutdown_function() as:
test.php
<html>
<body>
<?php include "functions.php";
global $connection;
$connection = pg_connect("host=localhost dbname=test user=test");
register_shutdown_function("close_con");
echo $connection." connection <br>";
echo pg_connection_status($connection)." status<br>";
pg_send_query($connection, "SELECT COUNT(id) FROM test");
echo pg_connection_busy($connection)." busy<br>";
$res = pg_get_result($connection);
echo pg_num_rows($res)." # rows<br>";
echo $GLOBALS["connection"]." GLOBALS<br>";
?>
</body></html>

The functions.php as above.

This also dose not work as, " The registered shutdown functions are
called after the request has been completed" (from
http://au2.php.net/manual/en/function.register-shutdown-function.php ).
I'm interpreting that as, any functions in the list will not be
called until the pg_send_query() or the pg_get_result(), not sure which,
returns. Then close_con() from functions.php is called via
register_shutdown_function(). Thus I'm unable to cancel the query :(

Any comments, suggestions or work arounds would be greatly appreciated.

I'm using PHP 4.2.3

Cheers
Noel

//-----------------------------------------------------------------------------
Muhyiddin A.M Hayat wrote:

> <body onunload(<?close_connection($connection)?>)>
>
> onunload above can't call function from PHP, but it called from
> javascript, you can create javascript function to call PHP
> function/file that you wanna PHP do
>
> <?php
> // close_connection.php
> include('connection.php');
> global $connection;
>
> function close_connection($connection){
> // close_connection here
> // .....
> // .....
> // .....
>
> }
>
> close_connection($connection);
> echo "<SCRIPT>window.close();</SCRIPT>";
> ?>
>
> ---------
>
> <html>
>
> <SCRIPT LANGUAGE="JavaScript">
> <!-- Begin
> function leave() {
> window.open('close_connection.php','','toolbar=no,menubar=no,location=no,height=10,width=10');
> }
> // End -->
> </SCRIPT>
>
> <body onunload = "leave()">
>
> </body>
> </html>
>
//----------------------------------------
ljb wrote:

noel(dot)faux(at)med(dot)monash(dot)edu(dot)au wrote:

>I have a small problem. Is it possible to cancel a running query when a
>person leave or pressed the stop button in my web page.
>...
>Have tried
><?php
> include "functions.php";
> $connection = pg_connect(host, dbname, user);
>?>
><html><body onunload(<?close_connection($connection)?>)>
>
>
> ...
>
> This will never work, as you found. The Javascript action is client-side
> and PHP is server-side.
>
> Try using register_shutdown_function() to register a function which will
> call pg_cancel_query(). As long as PHP isn't ignoring user aborts (see
> ignore_user_abort()), your query will be cancelled when the user hits STOP.
> Another way is to ignore user aborts, and while waiting for the
> asynchronous query to finish, keep calling connection_aborted() to see if
> you are still connected to the user; if not you cancel the query and exit.

--
Noel Faux
Department of Biochemistry and Molecular Biology
Monash University
Clayton 3168
Victoria
Australia

In response to

Browse pgsql-php by date

  From Date Subject
Next Message Jona 2003-12-16 10:05:59 Re: A progress page with PHP + PostgreSQL
Previous Message Rod K 2003-12-15 23:17:45 Re: What is wrong with pg_pconnect() ?