Page 1 of 1
agent/referer
Posted: Thu Apr 09, 2009 1:10 pm
by shaqa
pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:
Posting Code in the Forums to learn how to do it too.
i have code which filter traffic throught agent and referers.
the code before was denny/blocking specified agents and referers , i want to modify it to accept only from specified agents/referers.so if other agent persist exit ; will be activated
Code: Select all
$ar_agent = array("SampleAgent1","SampleAgent1");
$ar_badreferer = array("mysitename","mydomainname");
$display = 1;
$reason = "";
// Function HTTP_USER_AGENTS
function checkagent($agent) {
if (substr_count($_SERVER['HTTP_USER_AGENT'], $agent) <> 0) {
$bagent = 1;
}
return $bagent;
}
// Function HTTP_REFERER
function checkref($referer) {
if(substr_count($_SERVER['HTTP_REFERER'], $referer) <> 0) {
$breferer = 1;
}
return $breferer;
}
// Function Pesky Nulls!
function itsnull($danullvoid) {
if ($danullvoid == "") {
return false;
}
return true;
}
// Filter HTTP_USER_AGENT
foreach ($ar_agent as $agent_value) {
if (checkagent($agent_value) == 1) {
$display = 1;
break;
}
}
// Filter HTTP_REFERER
foreach ($ar_badreferer as $badreferer_value) {
if (checkref($badreferer_value) == 1) {
$display = 1;
break;
}
}
if(empty($display)){
$display = 0;
$reason .= "Bad Referer: ". $badreferer_value ." / ";
}
pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:
Posting Code in the Forums to learn how to do it too.
Re: agent/referer
Posted: Thu Apr 09, 2009 3:26 pm
by nyoka
How about the following:
Code: Select all
<?php
$ar_goodagents = array("SampleAgent1","SampleAgent1");
$ar_goodreferers = array("mysitename","mydomainname");
if (!in_array($_SERVER['HTTP_USER_AGENT'], $ar_goodagents) || !in_array($_SERVER['HTTP_REFERER'], $ar_goodreferers)) {
$display = 0;
$reason .= "Bad Referer: ". $badreferer_value ." / ";
}
?>
Re: agent/referer
Posted: Thu Apr 09, 2009 4:58 pm
by shaqa
how to accept only good agents and referes and decline all others.
Re: agent/referer
Posted: Fri Apr 10, 2009 11:10 am
by nyoka
What you originally asked for is "i want to modify it to accept only from specified agents/referers.so if other agent persist exit ; will be activated" which implies that you know what agents/referers you want to accept requests from, is this no longer the case?
Re: agent/referer
Posted: Fri Apr 10, 2009 1:04 pm
by shaqa
let me be more clear again,mayb my bad english....
i want to accept traffic only from agent and referers in list. if any other agent and referers will be outside the list will be disabled.
Re: agent/referer
Posted: Fri Apr 10, 2009 1:20 pm
by nyoka
The code I gave you checks both lists. To break it down more I have explained it further.
The IF statement will be TRUE if:
The content of $_SERVER['HTTP_USER AGENT'] doesn't exists in the array $ar_goodagents
OR
The content of $_SERVER['HTTP_REFERER'] doesn't exist in the array $ar_goodreferers
The arrays above are your lists of acceptable values. To make all the requests accepted that you want make sure all the lists are populated with all the accepted values by change the lines of code below.
Code: Select all
$ar_goodagents = array("SampleAgent1","SampleAgent1");
$ar_goodreferers = array("mysitename","mydomainname");
Note that now all browsers/devices pass the HTTP_REFERER global variable so personally I find it un-reliable using it.
Re: agent/referer
Posted: Fri Apr 10, 2009 5:00 pm
by shaqa
still doesnt work.
Re: agent/referer
Posted: Sat Apr 11, 2009 8:17 pm
by shaqa
below is my completed code, please somebody can help me how to accept ONLY agents pasted in $ar_agent $ar_badreferer , all others agents and referers must be disabled, being more clear,, the code below currently disable agents/referers access only as desrcibed in $ar_agent $ar_badreferer.
Code: Select all
require_once('Connections/default.php');
// Set These VARS
$banned_ips = file_get_contents("./ipjatba.txt");
$logfile = "./log.html";
$ipfile = "./ajpijat.txt";
$ar_agent = array("Firefox","Opera");
$ar_badreferer = array("Google","Yahoo");
$doubles = 0; // value of 0 or 1
$userid = "admin"; // Change this to the userid in your database
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
mysql_select_db($database_default, $default);
$query_Recordset1 = "SELECT * FROM sajti WHERE (userid = 'admin') AND (sajti.a_count < sajti.a_max) AND (sajti.a_active = '1') ORDER BY sajti.a_count";
$Recordset1 = mysql_query($query_Recordset1, $default) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
// Fixed Vars - Do NOT EDIT
$display = 1;
$reason = "";
// Function HTTP_USER_AGENTS
function checkagent($agent) {
if (substr_count($_SERVER['HTTP_USER_AGENT'], $agent) <> 0) {
$bagent = 1;
}
return $bagent;
}
// Function HTTP_REFERER
function checkref($referer) {
if(substr_count($_SERVER['HTTP_REFERER'], $referer) <> 0) {
$breferer = 1;
}
return $breferer;
}
function itsnull($danullvoid) {
if ($danullvoid == "") {
return false;
}
return true;
}
$ar_banned = explode("\n", $banned_ips);
$enduserip = $_SERVER['REMOTE_ADDR'];
list($eu_a, $eu_b, $eu_c, $eu_d) = split("\.", $enduserip);
foreach ($ar_banned as $val) {
list($banned_a, $banned_b, $banned_c, $banned_d) = split("\.", $val);
if (($eu_a == $banned_a) AND ($eu_b == $banned_b) AND ($val <> "")){
$reason .= "Banned IP (" . $val . ") / ";
// print "val=".$val;
}
}
if ($doubles == 1) {
$fs = file_get_contents($ipfile);
$fa = explode("\n", $fs);
foreach ($fa as $value) {
if ($value == $_SERVER['REMOTE_ADDR']) {
$reason .= "Already / ";
}
}
}
// Filter HTTP_USER_AGENT
foreach ($ar_agent as $agent_value) {
if (checkagent($agent_value) == 1) {
$display = 0;
$reason .= "Bad Agent: ". $agent_value ." / ";
}
}
// Filter HTTP_REFERER
foreach ($ar_badreferer as $badreferer_value) {
if (checkref($badreferer_value) == 1) {
$display = 0;
$reason .= "Bad Referer: ". $badreferer_value ." / ";
}
}
$calc_ctr = rand(1,100);
if ($reason == "") {
$display = 1;
$foundone = 0;
do {
if ($calc_ctr < $row_Recordset1['a_ctr']) {
$foundone = 1;
echo "display=1&banner=" . $row_Recordset1['a_url'];
$fh = fopen($logfile, 'a') or die("can't open file");
fwrite($fh,"\n<br>Referer: ");
fwrite($fh, ($_SERVER['HTTP_REFERER']) );
fwrite($fh,", ");
fwrite($fh, ($_SERVER['HTTP_USER_AGENT']) );
fwrite($fh,", ");
fwrite($fh, $_SERVER['REMOTE_ADDR']);
fwrite($fh,", ");
fwrite($fh, date('l jS \of F Y h:i:s A') );
fclose($fh);
if ($doubles == 1) {
$ipf = fopen($ipfile, 'a') or die("Can't Open File");
fwrite($ipf, $_SERVER['REMOTE_ADDR']);
fwrite($ipf, "\n");
fclose($ipf);
}
$newcount = $row_Recordset1['a_count'];
$newcount++;
$updateSQL = sprintf("UPDATE sajti SET a_count=%s WHERE affiliateid=%s",
GetSQLValueString($newcount, "int"),
GetSQLValueString($row_Recordset1['affiliateid'], "int"));
mysql_select_db($database_default, $default);
$Result1 = mysql_query($updateSQL, $default) or die(mysql_error());
}
} while ( ($row_Recordset1 = mysql_fetch_assoc($Recordset1)) && ($foundone == 0));
if ($foundone == 0) {
echo "display=0&banner=";
}
}
else {
//echo "display=0&ctr=". $ctr . "&banner=" . $arr_affiliate[$affiliate_id];
echo "display=0&banner=";
$fh = fopen($logfile, 'a') or die("can't open file");
fwrite($fh,"\n<font color=\"red\"><br><b>" . $reason);
fwrite($fh,"</b> ");
fwrite($fh,"Referer: ");
fwrite($fh, ($_SERVER['HTTP_REFERER']) );
fwrite($fh,", ");
fwrite($fh, ($_SERVER['HTTP_USER_AGENT']) );
fwrite($fh,", ");
fwrite($fh, $_SERVER['REMOTE_ADDR']);
fwrite($fh,", ");
fwrite($fh, date('l jS \of F Y h:i:s A') );
fwrite($fh, "</font>");
fclose($fh);
}
mysql_free_result($Recordset1);