In the last example the call to ignore_user_abort() is used incorrectly. The manual clearly states that is the first parameter is not given, only the value is returned!
Specify a boolean value like this,
ignore_user_abort(TRUE);
ignore_user_abort
(PHP 4, PHP 5)
ignore_user_abort — Active l'interruption de script sur déconnexion du visiteur
Description
int ignore_user_abort
([ string $setting
] )
Active l'option décidant si, lors de la déconnexion du client, le script doit poursuivre son exécution ou non.
Liste de paramètres
- setting
-
Si non définit, la fonction ne retournera que la configuration actuelle.
Valeurs de retour
Retourne la configuration précédente, sous la forme d'un booléen.
Exemples
Exemple #1 Example avec ignore_user_abort()
<?php
// Ignore la déconnexion de l'utilisateur et autorise
// le script à continuer de s'exécuter
ignore_user_abort();
set_time_limit(0);
echo 'Test du gestionnaire de connexion de PHP';
// Exécution d'une boucle infinie surveillant
// l'activité de l'utilisateur. Soit il click en dehors
// de la page, soit il click sur le boutton "Stop".
while(1)
{
// La connexion a-t-elle échouée ?
if(connection_status() != CONNECTION_NORMAL)
{
break;
}
// On attend 10 secondes
sleep(10);
}
// Si c'est atteint, alors l'instruction 'break'
// sera lancée depuis la boucle infinie
// Aussi, nous pouvons à ce niveau entrer des informations dans l'historique,
// ou exécuter d'autres taches nécessaires, sans pour autant être dépendant
// du navigateur.
?>
Notes
PHP ne détecte pas le fait que l'utilisateur stoppe la connexion avant qu'une tentative d'envoi est effectuée au client. La simple utilisation d'un "echo" ne garantie pas que l'information est envoyée, voir la fonction flush().
Voir aussi
- connection_aborted()
- connection_status()
- Gestionnaire de connexion pour une description complète du gestionnaire de connexion en PHP.
ignore_user_abort
adrian
26-Apr-2007 03:37
26-Apr-2007 03:37
spiritual-coder at spiritual-coder dot com
13-Jul-2006 09:45
13-Jul-2006 09:45
If you want to simulate a crontask you must call this script once and it will keep running forever (during server uptime) in the background while "doing something" every specified seconds (= $interval):
<?php
ignore_user_abort(); // run script in background
set_time_limit(0); // run script forever
$interval=60*15; // do every 15 minutes...
do{
// add the script that has to be ran every 15 minutes here
// ...
sleep($interval); // wait 15 minutes
}while(true);
?>
15-Dec-2005 12:44
"It will return the previous setting" -- not quite. It returns an int, not a bool (as per the syntax description)
Rustam
26-Jun-2005 04:57
26-Jun-2005 04:57
I wrote a simple function that can "spawn" another thread within the webserver by making async http request. The page that is being spawned can call ignore_user_abort() and do whatever it wants in the background...
<?php
function http_spawn($page)
{
$basepath=ereg_replace('[^/]*$', '', $_SERVER['PHP_SELF']);
$cbSock=fsockopen('localhost', $_SERVER['SERVER_PORT'], $errno, $errstr, 5);
if ($cbSock)
{
fwrite($cbSock, "GET {$basepath}{$page} HTTP/1.0\r\n"
."Host: {$_SERVER['HTTP_HOST']}\r\n\r\n");
}
}
?>
Example:
<?php
if ($search_initialized)
http_spawn("ftindex.php");
?>
qartis at qartis dot com
06-Sep-2004 04:22
06-Sep-2004 04:22
Note that the function name and description seem to be contradictory:
- ignore_user_abort (TRUE, I want to ignore the user's abort request)
- Set whether a client disconnect should abort script execution (TRUE, I want to set this behavior)
The function name is the authoritative one: a value of TRUE will ignore the user's abort.
plamen at pulsator dot com
29-Mar-2001 09:21
29-Mar-2001 09:21
The script should output something to the browser in order to abort. If there is no output the script keeps on running.
