pg.ddx.io  pgsql-php@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Noel <noel.faux@med.monash.edu.au>
To: Muhyiddin A.M Hayat <middink@indo.net.id>
To: pgsql-php@postgresql.org, ljb220@mindspring.com, Di Wu <ever_wudi@yahoo.com>
Subject: Re: Cancel a query when user leaves a web page
Date: Tue, 16 Dec 2003 18:58:17 +1100
Message-ID: <3FDEBB19.9050100@med.monash.edu.au> (raw)
In-Reply-To: <00aa01c3aec5$3fb5df60$2b179fca@middinkcomp>
References: <3FB984FA.1090003@med.monash.edu.au>
	<00aa01c3aec5$3fb5df60$2b179fca@middinkcomp>

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.faux@med.monash.edu.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

view thread (4+ messages)

Message-ID: <3FDEBB19.9050100@med.monash.edu.au>
Permalink:  ../3FDEBB19.9050100@med.monash.edu.au/
Also on:    postgresql.org/message-id/3FDEBB19.9050100@med.monash.edu.au

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-php@postgresql.org
  Cc: noel.faux@med.monash.edu.au, middink@indo.net.id, ever_wudi@yahoo.com
  Subject: Re: Cancel a query when user leaves a web page
  In-Reply-To: <3FDEBB19.9050100@med.monash.edu.au>
  References: <3FB984FA.1090003@med.monash.edu.au> <00aa01c3aec5$3fb5df60$2b179fca@middinkcomp> <3FDEBB19.9050100@med.monash.edu.au>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox