knpandey
knpandey

Reputation: 41

sending an alert messages from server to client automatically without postbacks

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

Answers (5)

Abadis
Abadis

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

Tural Ali
Tural Ali

Reputation: 23290

You're talking about long-polling.

"Long polling" is the name used to describe a technique which:

  • An AJAX request is made (utilizing a javascript framework such as jQuery) The server waits for the data requested to be available, loops, and sleeps (your server-side PHP script)
  • This loop repeats after data is returned to the client and processed (usually in your AJAX request's onComplete callback function)

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 :

  • PHP is made for fast execution (not for waiting)
  • PHP will force you to do some kind of polling on the server side and relying on sleep()
  • PHP will eat your RAM while so are spawning processes for each requests (Apache will do so)
  • Don't use Apache server for this purpose! Apache Server will be better able to handle tens of thousands of short finite connections better than a few hundred persistent connections. Regardless of which direction you go (long-polling vs ajax) You might want to think about setting up a lighter webserver dedicated to the chat. something like Lighttpd or Nginx which can have larger numbers of max_clients or a larger number of simultaneous requests given the same memory/CPU conditions.

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...

Instead you can use simple way

enter image description here

Take a look at this table.

You can do something like that

  1. 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

  2. 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

arunes
arunes

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

Andrei G
Andrei G

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

Sergii Kudriavtsev
Sergii Kudriavtsev

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

Related Questions