Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get around server flooding by users opening countless tabs?

Tags:

ajax

php

I have some kind of chat/forum application that checks for new messages using periodic polling (every 15 seconds) using jquery ajax. I was wondering if i can get around the issue of users who try to be 'funny' by loading several same browser instances, with lots of tabs, all pointing to the same application. Each tab is sending an ajax request, which potentially can overflow a server if several users start to do the same thing.

I do store sessions in a table, along with the last access time and IP address, which works fine as long as users don't use the same browser. I could store a unique identifyer that is sent using the ajax POST or GET request, but that would give problems if a regular (non abusing) user refreshes his page, which would then create a new identifyer.

This is not a real problem yet, but better catch it before someone thinks of abusing the system like this :) Any idea how to do this?

like image 870
Sempiterna Avatar asked Nov 03 '22 20:11

Sempiterna


1 Answers

One option could be to fetch data like so:

  • Your script is preparing to poll data. Before executing the request, write (with LocalStorage), a value saying that you're going to fetch data. localStorage.setItem("last-request-timestamp", new Date().getTime());
  • Poll for data. You get a result. Write that result to the localStorage: localStorage.setItem("latest-messages", ajax_result);
  • Check if a page is preparing to poll data by checking if localStorage.getItem("last-request-timestamp") is longer than 15 seconds ago. If so, go to step 1. If not, wait 15 seconds and check again.
  • Regardless if the current page polled for data or not, check the latest-messages variable and update the page.

Other pages will of course share the localStorage data. They won't get data if another page is fetching at the moment. If page #1 is closed, one of the other pages will continue to fetch data.

I haven't used LocalStorage before, but browser support seems decent enough. You should also be able to just use it as a key-value array: localStorage["last-request-timestamp"].

You can only store strings in localStorage, but you can of course serialize it into JSON.

like image 53
RickN Avatar answered Nov 12 '22 13:11

RickN