开发者

Handling situation where User closes browser in php

开发者 https://www.devze.com 2023-03-20 12:39 出处:网络
I have a buy.php page where user selects a product, enters some data like his name, address etc. Next he clicks on the \"Buy\" button to move to the Payment Gateway site for inputting his Credit Card

I have a buy.php page where user selects a product, enters some data like his name, address etc. Next he clicks on the "Buy" button to move to the Payment Gateway site for inputting his Credit Card no + CVV no etc. And at this point, without clicking on the 'Pay' button on this page, he closes the browser or his Computer gets switched off. This Transation ID is saved in Session.

How to track this situation and save it as "User Abort开发者_StackOverflowed" against his transaction ID in the Database in PHP?


The way we dealt with this issue was to keep the status of the transaction in the database as "incomplete" (or "aborted" in your case) from the beginning. When the payment is completed, the transaction status is changed to "completed".


You can't handle browser events (which is client-side) via php (server-side)

Use jQuery function .unload() which is supposed to be triggered on browser window closing

Documentation: http://api.jquery.com/unload/

Note: Nothing can take care about situation when Client Computer goes power-off instantly (not using Start > Switch Off or similar OS feature)

Note 2: Nothing in webpage can take care about crashed or killed browser


While you can potentially utilize Javascript to capture the browser's close event, unless you want to do something very quickly and aren't looking for any sort of feedback as to its success you might want to try a different approach.

From what you said above it would seem that you are trying to classify a given transaction as aborted, and you can do that simply by keeping track of different transaction stages in your database back-end. Have different stages for the given transaction and then set it to "aborted" if it was about to be processed and has been hung in that stage for a given amount of time.


Internally in PHP a connection status is maintained. There are 3 possible states:

0 - NORMAL 1 - ABORTED 2 - TIMEOUT

When a PHP script is running normally the NORMAL state, is active. If the remote client disconnects the ABORTED state flag is turned on. A remote client disconnect is usually caused by the user hitting his STOP button. If the PHP-imposed time limit (see set_time_limit()) is hit, the TIMEOUT state flag is turned on.

This page will answer your question I guess.


Approach suggested by Aleks G is the best - however, as secondary measure, you may introduce some kind of 'heartbeats' between browser and server. For example, issue AJAX requests over specified interval of time, letting server know that user is 'alive' (page is opened in his browser).

However, I repeat, this can be used only as secondary measure and you can't rely on it to set transaction status. There's no definite way to know whether user/browser is 'still there'.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号