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 Aborted" 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'.
Related
I want to create a bidding system where user can see the current price of items. And if any other user on any other location place a bid before me it should auto update bid in my browser.
I have read about autoupdate JS+Ajax functions but even if I place a 5 second timer to auto update the content on user's browser will it not put some extra load on server by making an ajax call every 5 second? Its a bidding system so user will have bids updating within 1-2 seconds so if i put an auto update ajax call for every 1-2 seconds it will put a lot of burden on server.
So I am wondering is there any better way to handle this type of stuff? how do twitter/facebook do update user's feeds?
AJAX or not, bidding systems always have high requests because people keeps refreshing the page to check for the latest bid information.
You can take a look and attempt long polling. Long polling a method where you "push" data from the server to the browser in response to the browser's HTTP request. It is a normal HTTP connection. This may reduce the number of requests sent from users to server, however you will still have many open and active connections between your users and your server.
You will want to look at long polling. In essence, this is how it works
On the server you need some sort of event mechanism (no probem with PHP)
Client (Browser) starts an AJAX request referencing a bidding item
Server checks for changes on the bid, if there is one, returns the request
If not, he waits for some time (minute range), waiting on an event concerning this bid
If such an event occurs, server returns the request with the info, if not he returns the request with "no bid" info
You might be able to get away with a streaming model...
Each JS client connects to the server once and keeps the conneciton open. As new events arrive at the server, they are broadcast to all the open connections in real time.
This is similar to the mechanism twitter uses to broadcast tweets.
Okay so I'm running into a small problem.
Basicly my whole website runs through the AJAX system, content is loaded in the middle page, and theres a left and right menu which dont refresh.
Currently I'm trying to look for a PHP->Ajax feature that refreshes the whole website incase a certain record changes in the MYSQL table
Okay so every user has a record called "State" which indicates the state of their account, this can be changed by anyone, for example the account gets shot and killed by someone. How do I make it so it checks what state you have and if it changes from the "standart" state that it performs a full page refresh.
I tried to find an answer for this everywhere but haven't been able to figure something out.
-----Edit-----
Okay so I'll also notify, I kind of know how to perform a full page refresh, and I know how to retrieve data from the mysql database, this isn't the problem.
I have a table with all the users accounts in it, one of the records for every user is called "State" everybodies state will be 1 which means alive. when its 0 it means its a dead account.
On a part of my website theres an auto refresh with always fetches data from the database every 5 seconds, to check if your online if you have money etc. it also checks what state you have.
the only thing I want to do, is that when it sees your state is 0, it performs a full page refresh, considering state 0 means death, you should be seeing a deathscreen, I want it to perform a full page refresh cause the menu's have to dissapear. and it has to redirect you to the deathpage.
You need long pooling / comet - basically you keep open connection between the client and the server, and when the state is changed, the server sends the response to the client.
Basically, you'll open a long pooling connection, sending the userid.
The server script receives the userid, and starts monitoring for changes for that user. If such change is detected, send the response.
If performance is concern, you can use Tornado web server. What's nice about it, is that you can post from another application to the web server, and it can detect which client is affected by the change and send response to that client.
We have a jquery/php/mysql system that allows a user to log in and review videos built by a system for online viewing. When a user begins reviewing a video, the video is marked as such. But now we've cornered ourselves into the classic browser-based application problem of the user navigating away or closing the browser without completing review. That video would then enter a state of limbo of constantly being reviewed, but never completed, and never re-entering the queue.
Options we have are:
Build a service (which we already have others) to find review sessions that are outside a duration boundary and reset them back into the queue.
Reset review sessions outside a duration boundary when that user logs in. Essentially, if a user locks out a video for review, it'll be unlocked the next time they log in.
A suggestion made to me was to use the php/apache session length and on expiration, reset any pending review jobs. I don't even know where to look to implement this as this is one project on a shared server, so it shouldn't be an apache config, but the reset mechanism would need to know the database credentials to be able to reset it...
The worst solution everyone hates is preventing the user from navigating away with javascript, asking "Are you sure?!"
This system is used by a few hired reviewers, so I'm not exactly dealing with the public here, but I can't prevent users from sharing logins for speedier review, which would knock out the 2nd option above because it would unlock a video being reviewed by someone else using the same login.
There are two good options that won't tax your server. Either:
Run a cron-job every hour looking for review sessions that are outside of the duration boundary. This has the advantage of being transparent to the end user. But it's possible to kill an active session if you're not careful (Suppose the user is operating in multiple tabs).
Prevent users from navigating away with JS. Honestly, this is what I would do since the user is reviewing the video (if they were just viewing it, that would be bad, but since they know they are supposed to be acting, it's ok). Just say If you leave now, the review will be canceled, are you sure?.
Honestly, I'd do option #2. SO uses it, and it works well here. It wouldn't be for every page, just those where there's an active review going on...
When a single video is reviewed on a single page by one person. You can capture the JavaScript unload event which will be fired when the page unloads and the client is leaving the page. Than you can change the state of that video or even show a dialog and let the user decide.
Maybe a ping-pong system may help. In the back you make AJAX calls binded to a video by an ident. When that activity stops, the users stopped. A background process can look for latest ping and when that is, for instance, a hour ago, change it's state.
Your first option is your best bet. You can have a javascript timer on the page updating the lastestActivity record for the video while the browser window is open So you won't get into the corner of a review that takes too long and the unlocker releases the lock prematurely.
Option 2 is problematic for several reasons, some have already been mentioned but taking into account that a reviewer might not log in for a few days (vacation?) will unlock a video for too long unnecessarily.
Option 4 (Javascript onBeforeUnload) Won't cover you in the common cases of a browser crash, OS crash or a power failure. but it is something you can implement in addition.
I have to save in DB the times when users entering and exiting my site,
with the enter is no problem, and so with exit by clicking my "log off" button,
but what with the situation that client exit by clicking X in the right corner?
how to handle this?
I tried use onUnload event but it works also when redirecting to other page in the site.
onClose event doesn't work.
any ideas??
Thanks,
sara, PHP and JS developer
As far as I know there is no standard way to check for the closing of the browser vs redirect to another page. Easiest thing to do would be to consider the user as logged out if there hasn't been any activity from the user in the past 20 min (or whatever you like).
There is no reliable way to do this. Consider other situations that might happen:
1. Client's browser crashes
2. Client's OS crashes
3. Client's internet connection breaks
4. Client's computer explodes
There is no way you can 'catch' these 'events' so the most reliable thing you can do is to do a timeout like nico suggests.
You can use JavaScript to send 'heartbeat' requests to your server as often as you like (too often and you will annoy people), and if you miss three heartbeats consider they left the page (or they home was bombed)
Suppose user requests an action, the page is equipped with ajax so the request has been made through javascript and the status of task are updating every 10 seconds.
The problem is if the user close the page at this time the task would be lost?
I want the php request, continue and complete even if the user closed the page.
How can I do that?
It's tough to be sure without a more intimate explanation. You may need to investigate cronjobs though, this is typically a server specific task in which set up depends on the kind of server you're running. If you have an admin control panel through your host it may have a section for setting up such tasks.
At the very least you could potentially detect partially finished work and then finish it up after a certain amount of inactive time from the user.
The other possibility is to simply do that check every page refresh, this would be server independent, but relies on being able to unreliably finish half-done tasks whenever a page is refreshed instead of guaranteeing it gets cleaned up at a specific time.