DevChunks

Ignore User Aborts with PHP

When you are developing or supporting a website, it does not always work perfectly. Sometimes a visitor will abort a script that is running and when that happens you want to try and avoid that causing a client disconnect. The ignore_user_abort() function determines whether the remote client can abort the running of the script.
This example demonstrates how to use this function. Without an input it will return the setting in the ini file.

{code type=php}<?php
ignore_user_abort();
?>{/code}

This should then return the value 0.
If you use this function, then PHP will not know that the user has tried to abort the connection until there is an attempt to send information back to the client. The use of an echo statement will not guarantee that information is sent.
This next example is a complete demonstration of how the ignore_user_abort() function works:

{code type=php}
<?php
// Ignores user abort – the script can run forever
ignore_user_abort(true);
set_time_limit(0);

echo ‘Connection Handling Text’;

// Runs a loop
while(1)
{
// Checks if the connection failed
if(connection_status() != CONNECTION_NORMAL)
{
break;
}
// Pause 10 seconds
sleep(10);
}
// The visitor broke while in the loop
// Perform other tasks independent of the browser
?>
{/code}

Share:

Leave a Reply

Your email address will not be published.

More Posts

How to use PHP Type Declarations

PHP has developed into a more strictly typed language in recent years. Type Declarations help with that and are a fairly new addition to PHP,

Iteration

If you have come to JavaScript from a background in Java or C, then you will like the variation on the for loop. It is

Context Object Manipulation

One of the key concepts of JavaScript is the keyword this, which signifies the JavaScript context object. If you write an inner function, you can access

Polymorphic Functions

It is possible to define multiple functions with the same name in JavaScript, and the last one of those that you define will be the