php code help

Hi,

How to make code to detect if a ip address is sending more requests; example: if any ip address send 10 requests in 2 seconds then code should catch it?

Any code idea or sample?

Thanks
LVL 5
sindhuxyzAsked:
Who is Participating?
 
Mohamed AbowardaConnect With a Mentor Software EngineerCommented:
You can use the database to store log for each IP Address requests, for example:
IP Address 127.0.0.1 made first request:
Add record:
----------------------------------
IPAddress          Count
----------------------------------
127.0.0.1            1

The same IP Address made another request, update the record
----------------------------------
IPAddress          Count
----------------------------------
127.0.0.1            2
0
 
Dave BaldwinFixer of ProblemsCommented:
Note that you will get a separate request for every file referenced on your page.  If you have 10 files including the main, the CSS file, javascript files, and image files, then you will get 10 requests just for one page.
0
 
sindhuxyzAuthor Commented:
We can conclude, how many files are referenced, just need sample to check if there are more request from one ip address,

0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
Dave BaldwinFixer of ProblemsCommented:
On http://www.php.net/manual/en/reserved.variables.server.php, look at 'REMOTE_ADDR'.  You can view it like below.  You would have to create a way to keep track of it in a file or a database.
<?php
echo $_SERVER['REMOTE_ADDR'];
?>

Open in new window

0
 
Beverley PortlockCommented:
SIgn up for Google Analytics - that will tell you how often a page is hit and can give IP and geo maps as well. All you need to do is to embed the UA code in the page.
0
 
sindhuxyzAuthor Commented:
Is it possible to use session values to store ip address woth request counts, if request count is greater than a value then refuse connection
0
 
Dave BaldwinFixer of ProblemsCommented:
If this is related to your question about getting 30,000 requests per second, PHP is not going to be fast enough to handle this.  You will need to look at methods in the server software or possibly the firewall that originally gets the requests.
0
 
InsoftserviceCommented:
Hi,
It depends upon ur server and has to be done by server admin.
But it can be even manage via the link provided. and later redirect it to the script where captcha validation is available..
Hope it helps u
0
 
sindhuxyzAuthor Commented:
@:Medo3337

I want to do what u have mentioned but using session variables instead of using database. Please let me know how to do this using session variables and what code will help
0
 
Mohamed AbowardaConnect With a Mentor Software EngineerCommented:
To do the same way using session variable use the follow code:
// Execute the following code when the visitor make any new request
session_start();
if (!isset($_SESSION['requestsCount']))
{
	$_SESSION['requestsCount'] = 1;
} else {
	$_SESSION['requestsCount']++;
}

// You can use $_SESSION['requestsCount'] to get the number of requests

Open in new window

0
 
Ray PaseurConnect With a Mentor Commented:
Is it possible to use session values to store ip address

Not if I understand your inquiry correctly.  A session is started per unique browser request.  So if I run a script that does 100 requests with 100 different file_get_contents() or CURL statements, you are totally dependent on my script to act like a well-behaved browser, accepting the session cookies and returning them to your server.  I do not believe that you can depend on that, and therefore the session is the wrong tool.  The data base is the right tool.

The $_SERVER["REMOTE_ADDR"] variable is not immutable, but it is probably good enough for most uses.  If you need more protection than this, consider adding a form to your script and requiring the client to submit a CAPTCHA test before proceeding.
0
 
sindhuxyzAuthor Commented:
thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.