Forum Moderators: coopster

Message Too Old, No Replies

stopping certain querys being logged by looking at the ip address

         

JuiceUK

2:11 pm on May 25, 2006 (gmt 0)

10+ Year Member



've found this function in a script I'm using - when someone uses this particular script it logs what their query in the search box was. I want to add to it. I want it to only log the query in the search box if its' not come from one of 4 ip address. I'm only interested in query's being logged outside of my organisation. This is the original part of the script.

function saveToLog ($query, $elapsed, $results) {
global $mysql_table_prefix;
mysql_query("insert into ".$mysql_table_prefix."query_log (query, time, elapsed, results) values ('$query', now(), '$elapsed', '$results')");
echo mysql_error();
}

As I only know the first sections of some of the ip addresses I put that in with xx's. This is my attempt belwo but it errors for fun. Can anyone rework it? Thanks for your efforts - really appreciated.

function saveToLog ($query, $elapsed, $results)

$ip = $_SERVER['REMOTE_ADDR'];

if (substr($ip, 0, 8)!="#*$!.#*$!.") or (substr($ip, 0, 3)!="xx.") or (substr($ip, 0, 12)!="xxx.xxx.xxx.") or (substr($ip, 0, 12)!="xxx.xxx.xxx.") {
global $mysql_table_prefix;
mysql_query("insert into ".$mysql_table_prefix."query_log (query, time, elapsed, results) values ('$query', now(), '$elapsed', '$results')");
echo mysql_error();
}

barns101

2:54 pm on May 25, 2006 (gmt 0)

10+ Year Member



One way is to put your IP addresses in an array and check the array before logging the search:


$your_ips = array('127.0.0.1', '0.0.0.0', '1.1.1.1', '2.2.2.2', '3.3.3.3');
if (in_array($REMOTE_ADDR, $your_ips))
{
// Do nothing
}
else
{
// Log the search
}

JuiceUK

10:18 am on May 30, 2006 (gmt 0)

10+ Year Member



Thanks vey much -

Could you help further - what if I only have half of the ip address for some of the ip addresses - this is becuase we have many ip addresses we use so I just want ot check does it have like xx.xx at the front if it does it's from us so don't bother to log it.

M

barns101

1:31 pm on May 30, 2006 (gmt 0)

10+ Year Member



I think that you could check the IP address using regular expressions and if it started #*$!.xxx then the query would not be logged. I'm no expert with regular expressions but something like this should work (replacing the xs with the start of your IPs):


if(ereg("^xxx.xxx", $REMOTE_ADDR))
{
// This is your IP so do nothing
}
else
{
// Log the search
}