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>
<?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>