Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7523527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:49:45+00:00 2026-05-30T02:49:45+00:00

I’m building a model view controller web application and I want to build a

  • 0

I’m building a model view controller web application and I want to build a request spam mechanism, why? Let me explain in short detail:

We got ourselves an ajax controller, every user input is received through ajax, no direct $_POST is done in my web application.

Let’s imagine a few actions of the Ajax controller where we want to put a spam prevention mechanism on:

class AjaxController{
    private function setPrevention($interval){
        $latestActionRequest = $_SESSION['requests'][$this->action];

        if($prevention === null){
            $_SESSION['requests'][$this->action] = array('latest' => microtime(), 'interval' => $interval
        } else {
            // Calc difference here, and check if the interval was within range, else
            // the user was requesting the action method to quickly.
        }
    }

    public function _postComment(){
        $this->setPrevention(1000);

        // Apply validation, on the $_POST array, insert the to database.
    }
}

So we got an action to post a comment, we only want to allow the user to post a comment each second, so we apply a very basic prevention mecanisch in our session.

Check the comment in the setPrevention method. I have got 2 questions, my first question is, is this mechanism a good idea? Or are there alternative better ways to build this?

Second question is, how do I check if the latest request was within the interval range? With
microtime – microtime I get the difference in seconds, but there are actions where I want to apply an 500 ms interval.

What I got so far:

$_SESSION['requests']['postComment'] = array('latest' => microtime(true), 'interval' => 1000);
$difference = ($_SESSION['requests']['postComment'] - microtime(true));

At this point $difference gives back float(106.984388113) (waited 106 seconds)
But we want to get the microtime difference because our interval is 1000 (which is 1 second not 1000)

I hope my question was clear, thanks for help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T02:49:47+00:00Added an answer on May 30, 2026 at 2:49 am

    The first answer should help with the sub second timing. microtime(true)

    In response to the edit: The decimal portion is the sub second timing. the .984 is the part you’re looking for in terms of precision. .500 is your 500 milliseconds. I would recommend setting up your intervals in terms of this float value.

    You could multiply the difference by 1000, but if you ever want to adjust to a precision greater or less than milliseconds it’ll be even more confusing. And I propose making it as easy to adjust this later as you can.

    As for the mechanism for spam prevention there are a lot of options, but they will depend on the specifics of what your application needs.

    The best advice I could give you for that is to abstract it in some way that it can get more information about the AJAX call than you think you may need now and build a simple system that works now and log the information about requests for review.

    The biggest problem with spam prevention is preventing legitimate users and annoying them. So the best thing you can do is make it easier for yourself or another developer to swap out your mechanism in the future. You also need to have logs of the requests to be able to determine what kinds of requests are being stopped or not by the spam prevention.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
i want to parse a xhtml file and display in UITableView. what is the
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.