|
|
Kapitola 22. Obsluha spojen�Pozn�mka: N�sleduj�c� text plat� pro verzi 3.0.7 a vy���.
Stav spojen� se v PHP intern� sleduje. Jsou t�i mo�n� stavy:
P�i norm�ln�m b�hu PHP skriptu je aktivn� stav NORMAL. Pokud se klient
odpoj�, nastav� se p��znak ABORTED. K odpojen� vzd�len�ho klienta typicky
doch�z�, kdy� u�ivatel zm��kne tla��tko STOP. Pokud se dos�hne �asov�ho
limitu (viz set_time_limit()), nastav� se stavov� p��znak
TIMEOUT.
M��ete se rozhodnout jestli chcete, aby odpojen� klienta zp�sobilo p�ed�asn�
ukon�en� va�eho skriptu. N�kdy je u�ite�n� nechat skripty dob�hnout do konce,
p�esto�e nen� vzd�len�ho browseru, kter� by p�ij�mal v�stup. V�choz� chov�n�
je nicm�n� takov�, �e p�i odpojen� vzd�len�ho klienta dojde k ukon�en� b�hu
skriptu. Toto chov�n� se d� zm�nit skrze konfigura�n� direktivu
ignore_user_abort v php3.ini, odpov�daj�c� direktivu php3_ignore_user_abort
v .conf souboru Apache, �i funkci ignore_user_abort().
Pokud ned�te PHP pokyn ignorovat odpojen� u�ivatele a ten se odpoj�, v�
skript se ukon��. V�jimkou je, pokud m�te pomoc�
register_shutdown_function() zaregistrovanou funkci pro
proveden� p�i ukon�en� skriptu. V tom p��pad�, pokud vzd�len� u�ivatel zm��kne
tla��tko STOP, p�i dal��m pokusu tohoto skriptu odeslat v�stup PHP detekuje,
�e spojen� bylo zru�eno, a zavol� se funkce zaregistrovan� pro proveden� p�i
ukon�en� skriptu. Tato funkce se zavol� tak� na konci b�hu skriptu kon��c�m
norm�ln�, tak�e pokud chcete po zru�en�m spojen� ud�lat n�co jin�ho, m��ete
pou��t connection_aborted(). Tato funkce vr�t�
TRUE, pokud bylo spojen� zru�eno.
V� skript m��e tak� ukon�it vestav�n� ��ta� �asu. V�choz� �asov� limit je
30 sekund. To se d� zm�nit max_execution_time direktivou v php�.ini nebo
odpov�daj�c� php3_max_execution_time direktivou v .conf souboru Apahe, �i
vol�n�m funkce set_time_limit(). Kdy� ��ta� �asu dob�hne,
skript se ukon��, a jako ve v��e uveden�m p��pad� u�ivatelsk�ho odpojen�,
pokud je zaregistrovan� funkce pro proveden� p�i ukon�en� skriptu, tato se
zavol�. Uvnit� t�to funkce m��te zkontrolovat, jestli jej� zavol�n� zp�sobilo
dob�hnut� ��ta�e �asu zavol�n�m funkce connection_timeout().
Tato funkce vr�t� TRUE, pokud vol�n� funkce registrovan�
pro proveden� p�i ukon�en� skriptu zp�sobilo dob�hnut� ��ta�e �asu.
Skute�nost� hodnou pov�imnut� je, �e stavy ABORTED a TIMEOUT mohou b�t
aktivn� sou�asn�. Mo�n� je to v p��pad�, �e na��d�te PHP ignorovat odpojen�
u�ivatee. PHP i tak bude v�d�t, �e u�ivatel p�eru�il spojen�, ale skript
pob�� d�l. Pokud potom dos�hne �asov�ho limitu, bude ukon�en, a zavol� se
va�e funkce pro proveden� p�i ukon�en� skriptu, pokud existuje. V tomto
okam�iku zjist�te, �e jak connection_timeout(), tak
connection_aborted() vracej� TRUE.
Oba stavy m��ete zkontrolovat jedin�m vol�n�m funkce
connection_status(). Tato funkce vrac� bitov� pole
aktivn�ch stav�. Tak�e nap��klad, pokud jsou aktivn� oba tyto stavy, vr�t�
3.
Lee
18-Sep-2004 10:16
The point mentioned in the last comment isn't always the case.
If a user's connection is lost half way through an order processing script is confirming a user's credit card/adding them to a DB, etc (due to their ISP going down, network trouble... whatever) and your script tries to send back output (such as, "pre-processing order" or any other type of confirmation), then your script will abort -- and this could cause problems for your process.
I have an order script that adds data to a InnoDB database (through MySQL) and only commits the transactions upon successful completion. Without ignore_user_abort(), I have had times when a user's connection dropped during the processing phase... and their card was charged, but they weren't added to my local DB.
So, it's always safe to ignore any aborts if you are processing sensitive transactions that should go ahead, whether your user is "watching" on the other end or not.
ej at campbell *dot* name
12-Feb-2004 01:01
I don't think the first example given below will occur in the real world.
As long as your order handling script does not output anything, there's no way that it will be aborted before it completes processing (unless it timeouts). PHP only senses user aborts when a script sends output. If there's no output sent to the client before processing completes, which is presumably the case for an order handling script, the script will run to completion.
So, the only time a script can be terminated due to the user hitting stop is when it sends output. If you don't send any output until processing completes, you don't have to worry about user aborts.
pulstar at mail dot com
07-Aug-2003 06:32
These functions are very useful for example if you need to control when a visitor in your website place an order and you need to check if he/she didn't clicked the submit button twice or cancelled the submit just after have clicked the submit button.
If your visitor click the stop button just after have submitted it, your script may stop in the middle of the process of registering the products and do not finish the list, generating inconsistency in your database.
With the ignore_user_abort() function you can make your script finish everything fine and after you can check with register_shutdown_function() and connection_aborted() if the visitor cancelled the submission or lost his/her connection. If he/she did, you can set the order as not confirmed and when the visitor came back, you can present the old order again.
To prevent a double click of the submit button, you can disable it with javascript or in your script you can set a flag for that order, which will be recorded into the database. Before accept a new submission, the script will check if the same order was not placed before and reject it. This will work fine, as the script have finished the job before.
Note that if you use ob_start("callback_function") in the begin of your script, you can specify a callback function that will act like the shutdown function when our script ends and also will let you to work on the generated page before send it to the visitor.
| |