Reply
Capturing IP address & blocking IP address
Old 06-29-2009, 07:56 PM Capturing IP address & blocking IP address
Extreme Talker

Posts: 178
Trades: 0
I'm using a php script for my web site which has registration, log-in, etc.
When I asked how I might be able to capture the registrants IP address upon registration, I was given this code and this advice:

Code:
$userip = $_SERVER[REMOTE_ADDR];
mysql_real_escape_string($userip);
"Remember when you store these values in the database to use these two functions INET_ATON() and INET_NTOA(). One is for inserting the IP and one is for extracting it."

I don't know what INET_ATON() and INET_NTOA() are, but my goal is to be able to have a users IP address just in case he spams my site, then he can be blocked, based on the thinking that typically one user has one computer.

can someone tell me how I can block an Ip address?
Any thoughts/replies/suggestions will be welcome.
chrisj is offline
Reply With Quote
View Public Profile
 
 
When You Register, These Ads Go Away!
Old 06-30-2009, 12:32 AM Re: Capturing IP address & blocking IP address
orionoreo's Avatar
Extreme Talker

Posts: 238
Name: Jerry
Trades: 0
I have a row in all my tables for IP just to track a record of activities incase somebody shared an IP with somebody who was spamming.

I call a function in my header:

PHP Code:
if ($_SERVER['HTTP_X_FORWARD_FOR']) {
   
$ip htmlentities($_SERVER['HTTP_X_FORWARD_FOR']);
  } else {
   
$ip htmlentities($_SERVER['REMOTE_ADDR']);
  } 
and everytime I insert anything to my DB I just insert IP in the end

PHP Code:
mysql_query("INSERT INTO table(field1, field2, ip) VALUES('$one', '$two', '$ip')"); 
the mini cms I built can track activities and send an ip to my ban table.

In the header of the public facing page I call the ban script

PHP Code:
//IP CHECK
 //Getting the IP
 
$ip $data_func->getip();
 
 
//Getting Banned Ips
 
$get_ban "SELECT * FROM $ban_table WHERE ip='$ip'";
 
$get_ban_query mysql_query($get_ban);
 
$get_ban_num mysql_num_rows($get_ban_query);
 
 
//If They Have Been Banned
 
if ($get_ban_num>0) {
  
$reason '';
  for (
$a=0$a<$get_ban_num$a++) {
   
//Check If They Have Been Unbanned
   
$ban_reason mysql_result($get_ban_query$a'ban_reason');
   
$reason .= $ban_reason;
  }
  
  if (
$reason!='') {
   echo 
'Sorry your IP has currently been suspended from usage at the moment. Please Contact an admin at: to resolve the issue.';
   exit();
  }
 } 

something like the one above... because all of my pages call for the header script and all my headers require the ban script if they are banned they can't load past the exit();... thats something I kinda came up with but not sure if it follows best practices... I'm sure the other members can also shed some light on this
orionoreo is online now
Reply With Quote
View Public Profile
 
Old 07-02-2009, 08:07 AM Re: Capturing IP address & blocking IP address
EdB
Skilled Talker

Posts: 64
Name: Ed Barnett
Trades: 0
The INET_ATON() and INET_NTOA() functions are explained at this page;

http://arjen-lentz.livejournal.com/44290.html

These functions allow you to save the ip address as an integer (without the '.'s) meaning retrieving them is quicker that looking for VARCHAR type. Have a read of the page and see what you think
__________________
Printer Cartridges Cardiff
EdB is offline
Reply With Quote
View Public Profile Visit EdB's homepage!
 
Old 07-03-2009, 01:24 PM Re: Capturing IP address & blocking IP address
orionoreo's Avatar
Extreme Talker

Posts: 238
Name: Jerry
Trades: 0
thannks EdB
orionoreo is online now
Reply With Quote
View Public Profile
 
Old 07-07-2009, 04:28 AM Re: Capturing IP address & blocking IP address
Novice Talker

Posts: 5
Name: gaurav
Trades: 0
Capturing IP Address:- Use this code
Quote:
$ipadd = $_SERVER[REMOTE_ADDR];
to capture the ip address of the customer and everything worked great.

We just added an SSL certificate to make the site more secure and our order page is showing as HTTPS as supposed to HTTP. Now all the orders we receive have the same ip address. I checked the ip address and it is host.

I have no idea how to start troubleshooting. If this is a server problem or the SSL, or wrong php code.

Blocking IP address:- This tiny article was born after one incident for my web site, which was hacked by a turkey group, so I can't access /read my web site, so I did some coding to redirect the page when the particular IP was detected by PHP code, ok let’s start with general hacking information...


Dealing with hack attempts, evil web bots, and worms has been an ongoing headache.

Most of these problems come from dynamic IP addresses, so simply blocking the offender is only a temporary solution, and we may use to Examining logs and putting blocks in place is time consuming. Remembering to remove blocks on dynamic IP addresses is also a problem.


So we can block particular IP/ranges, even the proxy IP through the following simple code
Quote:
<?php
$ip_proxy=$_SERVER ["HTTP_X_FORWARDED_FOR"];

?>
The above line refers the predefined variable in PHP $_SERVER is an array containing information such as headers, paths, and script locations. The entries in this array are created by the web server.



The $_SERVER ['HTTP_X_FORWARDED_FOR'] header giving the IP address of the connection that it proxies, so we use to separate the IP and it's proxy address by using explode function., also we can get the IP address by using $_SERVER['REMOTE_ADDR'] but which is not worth full in web site hosted by sub domain.


I hope you all know about explode function that is Split a string by string
Quote:
<?php

$tnt=explode (',',$ip_proxy);

$ip=explode ('.',$tnt[0]);

$proxy=explode ('.',$tnt[1]);

?>
next thing is we should block all IP address which are in the text file, so we should enter the IP addresses which are to be blocked , here I have specified some turkey IP address in the text file which doesn’t have full structure of IP address format ,only I took three digit ,you can change code for checking full format of IP address !.
Quote:
<?php

$filename="input.txt";//text file

$lines = array (); //set as array

$file = fopen ($filename, "r"); //Open the file for reading only

while (! feof ($file)) { //read file line by line into a new array element

$lines [] = fgets ($file, 4096); //Gets line from file pointer

}

$x = count ($lines);

for ($y = 0; $y < $x; $y++) {

if((trim($lines[$y])==$ip[0])||(trim($lines[$y])==$proxy[1]))//check the IP/proxy address
{ echo 'Banned';//if IP match the listed IP means ,you can redirect/do some function here .

}

else { echo 'welcome'; }

}

?>
gkumar is offline
Reply With Quote
View Public Profile
 
Old 07-07-2009, 08:37 AM Re: Capturing IP address & blocking IP address
tripy's Avatar
Do not try this at home!

Posts: 3,139
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Ahhh, this reminds me of the time I was coding in PHP.
Python is so much elegant when it comes to this...
Code:
f=open('block.txt','r')
blocked=''
for line in f:
  blocked='\n'.join( (blocked, line) )

if ip.tolower().strip() in blocked:
  //access denied
else:
  //access granted
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Reply     « Reply to Capturing IP address & blocking IP address
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 



Page generated in 0.14054 seconds with 13 queries