Solved

php code help

Posted on 2011-03-13
13
347 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:sindhuxyz
  • 4
  • 3
  • 2
  • +3
13 Comments
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 35123692
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
 
LVL 5

Author Comment

by:sindhuxyz
ID: 35123752
We can conclude, how many files are referenced, just need sample to check if there are more request from one ip address,

0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 35123817
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 35123958
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
 
LVL 5

Author Comment

by:sindhuxyz
ID: 35123976
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
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 35123989
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
 
LVL 12

Accepted Solution

by:
Mohamed Abowarda earned 250 total points
ID: 35124882
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
 
LVL 15

Assisted Solution

by:Insoftservice
Insoftservice earned 125 total points
ID: 35125760
0
 
LVL 15

Expert Comment

by:Insoftservice
ID: 35125767
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
 
LVL 5

Author Comment

by:sindhuxyz
ID: 35126975
@: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
 
LVL 12

Assisted Solution

by:Mohamed Abowarda
Mohamed Abowarda earned 250 total points
ID: 35127732
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
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 125 total points
ID: 35127937
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
 
LVL 5

Author Closing Comment

by:sindhuxyz
ID: 35236777
thanks
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question