I have a
PHP counter on my website and it writes the count to a text file as it incraments, but it also writes the visiting person's IP to another text file. I want to be able to write the IP's to an
XML file, anyone know how I could edit my script to do that? Here's a copy of my current script (counter.
php)
- PHP: Select all
<?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']);
$banned = array('24.000.00.00', '142.00.00.00');
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'];
}
$string = file_get_contents('txtfolder/ipaddresses.txt');
$pos = strpos($string , $user_ip);
$ip_file = "txtfolder/ipaddresses.txt";
$ip = fopen($ip_file, "a+");
if ($pos === false) {
fwrite($ip, "$user_ip\n");
fclose($ip);
}
}
?>
This is kinda what I wanted it to look like:
- Code: Select all
<?xml version="1.1"?>
<iplist>
<ipaddresses>
<ip address="000.000.000.000">
<ip address="999.999.999.999">
</ipaddresses>
</iplist>
I was hoping to add other values to the ip field later like:
browser="IE"
referer="google.com"
but those are another issue for a later date, unless anyone can suggest a way to do that aswell
Thanks!