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 6894413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:46:46+00:00 2026-05-27T06:46:46+00:00

I wish to ask if the code written below has any holes (probably it

  • 0

I wish to ask if the code written below has any holes (probably it will be useful for someone else). This code checks if more than 5 attempts within the last 5 minutes were made to login. Checked by ip and by userid. Every attempt to log in is stored in history table (MySQL).

history table is needed only for the purpose of CAPTCHA displaying.
ENGINE=InnoDB is chosen. (Do you recommend to use MyISAM for better performance? Would you recommend to put index on Time or userid or remote_addr?)

Any suggestions about optimization? Possible holes?
Thank you.

$query1="SELECT id FROM history WHERE (Time>date_sub(now(), interval 5 minute)) AND remote_addr='".ip2long($_SERVER['REMOTE_ADDR'])."' limit 6"; //limit 6 is for performance, 6+ means 6, we only care if it less than 5
$result1=mysql_query($query1);
//so we know mysql_num_rows($result1)


$query2="SELECT id FROM history WHERE (Time>date_sub(now(), interval 5 minute)) AND userid='".$userid."' limit 6";//limit 6 is for performance, 6+ means 6, we only care if it less than 5
$result2=mysql_query($query2);
//so we know mysql_num_rows($result2)



$attempts=0;//we want to set up $attempts=maximum of (mysql_num_rows($result1), mysql_num_rows($result2))
if($result1) {
    $attempts=mysql_num_rows($result1);
}
if($result2 && $attempts<mysql_num_rows($result2)) {
    $attempts=mysql_num_rows($result2);
}

//if $attempts is more than 5, then CAPTCHA should be displayed, if less then it should not

P.S. Do you still recommend to put usleep(100000) or any other method to prevent bruteforce in case if somebody writes the algorythm one day (until captcha again gets stronger) to hack captchas?

Added: thank you for asking, yes, $userid is cleaned from bad symbols.

  • 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-27T06:46:47+00:00Added an answer on May 27, 2026 at 6:46 am

    No security issues if $userid is sanitized, but I would offer the the following changes to make the code more efficient: Use MySQL’s COUNT function instead of returning the six ID numbers. If you don’t need to use the IDs for anything, don’t retrieve them. Also, I see no reason to query the database twice. Pare it down a bit.

    The code below is shorter, faster and safer (with mysql_real_escape_string() — I’d recommend taking a look at that function). We eliminate a query, a couple of if statements and save on sending the unnecessary data across the network:

    $userid = mysql_real_escape_string('...');
    $ip = ip2long($_SERVER['REMOTE_ADDR']);
    
    $q = "SELECT COUNT(*) AS cnt FROM history 
      WHERE Time > date_sub(now(), interval 5 minute)
      AND (remote_addr='$ip' OR userid='$userid')
    ";
    $r = mysql_query($q);
    
    // first value, first row (we only get one using count)
    $attempts = mysql_result($r, 0, "cnt");
    
    // if $attempts is more than 5, then CAPTCHA should be displayed ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't wish to create a wall of code so if you need any
Google ask you to add a javascript code to your page and it will
I wish to test a function that will generate lorem ipsum text, but it
I have a resource /system/resource And I wish to ask the system a boolean
I read this article which is about Managing heap memory written by Randy Kath.
I've encountered this problem a few times, and now I finally decided to ask,
I wish I could solve this by myself, but I have never quite grasped
This feels quite complicated to ask, and whilst the solution seems simple, the shear
Guys i think im lost in my own code, I will try explain exactly
My wish is not common, I guess, so I have to ask for recommendations.

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.