Reputation: 41
I am trying to implement a feature on my e-commerce website but I have no idea how to do it or where to begin with.
I want to alert my clients who are already verified and logged into my website and if somebody else with their (logged in users') login credentials with the intention of hacking or any other security risk, tries to login again with any other computer or browser.I can alert the already logged in user to change the password as soon as possible for better security or if it is the same user who is trying to log in from multiple clients then he will be restricted.
I will maintain a list of already logged in users in my database and will verify if the user is already logged in. But how can i send the alert dynamically without the user raising any postbacks from client side because as soon as the other login attempt is made my script should alert the already logged in user immediately.
I hope I have made my requirements clear. If anything is vague please comment and I shall clear.
I am using PHP 5.3 and MySQL 5.5. and the site will be hosted on a linux hosting.
I think AJAX will help but how. I am searching for a solution for many days now but no luck.
Please help.
Upvotes: 4
Views: 4467
Reputation: 2841
There is a technique in ASP called Signalr. you have to find an alternative to it in php which is I think Sockets.Maybe Ratchet or Wrench do the work. Read Here
Upvotes: 0
Reputation: 23290
"Long polling" is the name used to describe a technique which:
This essentially simulates a continuous real-time stream from the client to the server. I wouldn't do it in PHP for many reasons. Here are some :
But you can do it, using sleep, polling a database (or better a cache APC/Memcache).
If you want to do something like that jump into some technologies that can deal with events : Python (Tornado, gevent, eventlet, Twisted, …), Ruby (Eventmachine, …), Erlang, Scala, Server Side JavaScript (node.js, …), Comet...
Take a look at this table.
You can do something like that
Create some db table named, for ex. log
and record some various data (such as ip, sign-in date... ) when user signs in to your site. Leave signout_date field empty. (when user signs out just update this table and place current date) So if someone is on your site, signout date field must be empty
Then in every user activity, check your table for user_id
: if there is more than rows with same user_id
and empty signout date field. Then just notify user that, another pc signed in with your credentials.
Upvotes: 2
Reputation: 3524
When user logged in write user session id in user table. And check every 5-10 secs or value you want with ajax request is current session id is equal with in database.
If these values are not same, that means someone logged in with same user credentials.
Upvotes: 0
Reputation: 1590
Using ajax you can do a continuous poll at a url where you can display whether the alert needs to be fired or not. You could do this once every few seconds, or an interval of your choosing.
Upvotes: 0
Reputation: 10532
You're looking for a technique called Server Push.
TLDR: Make a server method which will accept request then block execution until some defined time elapses or some server-side event happens. Return different responses to client depending on whether it was server-side event or just timeout. From client - do AJAX call to this method with long timeout set and process the response.
Please check my answer for similar question.
UPD: Also, as @AndreiG suggested, you can implement similar functionality with continious polling. This is less complex yet less responsive to server-side events.
Upvotes: 0