Click to See Complete Forum and Search --> : PHP/XML referer script problem


Brad_Armitage
10-04-2005, 12:26 PM
Here's a copy of my script:


<?php
session_start();
session_register('counter');
$CountFile = "txtfolder/counter.txt";
$Count = file($CountFile);
$Count = implode("", $Count);
if(!$_SESSION['counter']){
$_SESSION['counter'] = true;
$OpenFile = fopen($CountFile, "r+");
$Count++;
$user_ip = GetHostByName($_SERVER['REMOTE_ADDR']);
$browser = get_browser($_SERVER['HTTP_USER_AGENT']);
$banned = array('66.67.6.7', '60.69.6.9');
if (!in_array($user_ip, $banned)) {
if($OpenFile){
fwrite($OpenFile, $Count);
fclose($OpenFile);
}
}
//
if (isset ($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$user_ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$user_ip = $_SERVER['REMOTE_ADDR'];
}
//
if ( strpos($_SERVER['HTTP_USER_AGENT'], 'Gecko') )
{
if ( strpos($_SERVER['HTTP_USER_AGENT'], 'Netscape') )
{
$browser = 'Netscape';
}
else if ( strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') )
{
$browser = 'Firefox';
}
else if ( strpos($_SERVER['HTTP_USER_AGENT'], 'Safari') )
{
$browser = 'Safari';
}
else
{
$browser = 'Mozilla';
}
}
else if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') )
{
if ( strpos($_SERVER['HTTP_USER_AGENT'], 'Opera') )
{
$browser = 'Opera';
}
else
{
$browser = 'Internet Explorer';
}
}
else
{
$browser = 'Other Browsers';
}
//
$domain = 'bradleya.ca';
function refer(&$ip, $domain, $referer)
{
$referer = isset($_SERVER['HTTP_REFERER']) && strpos($_SERVER['HTTP_REFERER'],$_SERVER['HTTP_HOST']) === FALSE ? $_SERVER['HTTP_REFERER'] : '';
$referer = parse_url($referer);
$referer = $referer['host'];
$ip->set_attribute('referer',$referer);
}
//
$string = file_get_contents('txtfolder/ipaddresses.xml');
$pos = strpos($string , $user_ip);
$ip_file = "txtfolder/ipaddresses.xml";
$document = &domxml_open_file($ip_file);
$tags = &$document->get_elements_by_tagname('ipaddresses');
if(empty($tags))
{
}
else
{
$ip_addresses = &$tags[0];
$ip = &$document->create_element('ip');
$ip->set_attribute('address', $user_ip);
$ip->set_attribute('browser', $browser);
refer($ip, $domain, $_SERVER['HTTP_REFERER']);
$ip_addresses->append_child($ip);
$document->dump_file($ip_file, false, true);
}
}
?>


The xml file stores the visitor's IP, browser, and refering site/search engine. The only thing I can't get it to do is after it opens the xml file, I want it to take the IP's in the $banned array above and prevent them from being stored in the file, so every time the people with those ip's visit my site, it skips over them. It already prevents my counter from incramenting when those IP's visit the site, and the text file I used before this xml file used to only store an IP once, or if it was one of the IP's in the $banned array it wouldn't store it at all. How can I get it to do the same thing using my xml file? (those are example IP's)

Here's an example of my xml file:


<?xml version="1.0" encoding="iso-8859-1" ?>
<iplist>
<ipaddresses>
<ip address="00.11.22.33" browser="Internet Explorer" referer="www.metacrawler.com" />
</ipaddresses>
</iplist>

Brad_Armitage
10-06-2005, 07:09 AM
anyone?

LiLcRaZyFuZzY
10-06-2005, 07:17 AM
cant you just check if the ip adress is in the array by looping through?

LiLcRaZyFuZzY
10-06-2005, 07:25 AM
something like that

<?php
$array = array("127.0.0.1", "80.239.110.4", "192.168.2.1");

$ip_tocheck = "192.168.2.1";

function check_ip_array($ip_tocheck, $array){
foreach($array as $key => $values){
if($ip_tocheck == $values){
return true;
}
}
}

if(check_ip_array($ip_tocheck, $array)){
// dont write to xml
echo "IP banned";
}else{
// write to xml
echo "IP not banned";
}
?>

Brad_Armitage
10-06-2005, 08:13 AM
I'm not sure I understand, how will looping through check the array?
Here is my array and if statement that used to skip over certain ip's:


$banned = array('66.67.6.7', '60.69.6.9');
if (!in_array($user_ip, $banned)) {
if($OpenFile){
fwrite($OpenFile, $Count);
fclose($OpenFile);
}
}


Since I am no longer writing ip's to a text file, this only prevents the ip from incramenting the counter. It doesn't seem to do what I want it to in the xml file!

LiLcRaZyFuZzY
10-06-2005, 08:16 AM
oops, i wasn't aware of the existence of the in_array function, sry

well, just put the whole "write to xml" code into that if statement

Brad_Armitage
10-06-2005, 08:31 AM
ok I tried that but it didn't work and the counter incramented as well when it's supposed to skip that ip, maybe I inserted it into the wrong spot, could you have a look at my code and show me where I should be placing it?

Brad_Armitage
10-06-2005, 10:07 AM
nvm I figured out what you meant thanks! :)

LiLcRaZyFuZzY
10-06-2005, 10:09 AM
sure it does, you have set no condition
you should first thing check if the ip is banned, then if it is, continue (write to text file and xml), if it is not, dont

LiLcRaZyFuZzY
10-06-2005, 10:12 AM
ok!