...

View Full Version : Recording IP addresses with PHP??



Brad_Armitage
05-26-2004, 06:11 PM
Here I have a PHP session counter that also records the IP of a person visiting the site in a text file:



<?php
session_start();
session_register('counter');
$CountFile = "folder_name/counter.txt";
$Count = file($CountFile);
$Count = implode("", $Count);
if(!$_SESSION['counter']){
$counter = true;
$OpenFile = fopen($CountFile, "r+");
$Count++;
$user_ip = GetHostByName($_SERVER['REMOTE_ADDR']);
$banned = array('142.67.105.80', '142.67.105.81');
if (!in_array($user_ip, $banned)) {
if($OpenFile){
fwrite($OpenFile, $Count);
fclose($OpenFile);
}
}

// loads the ip file into an array
$file = file("folder_name/ipaddress.txt");
// checks to see if the users ip is already in the text file
if (!in_array($user_ip, $file) {
$ip_file = "folder_name/ipaddress.txt";
$ip = fopen($ip_file, "a+");
fwrite($ip, "$user_ipn");
fclose($ip);
}
}
?>


What I want it to do is check to see if the IP address from the current session is already recorded in the text file, and if it is, don't record it again, if not record it once then start on a new line. It works well except it still records the IP address from a session more then once. HELP would be great!! :D

raf
05-26-2004, 08:14 PM
why not simply check if the ip occurs with a strpos()
the way you grab the IP also looks strange to me.


if (isset ($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$user_ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$user_ip = $_SERVER['REMOTE_ADDR'];
}
$string = file_get_contents('folder_name/ipaddress.txt');
$pos = strpos($string , $user_ip);
if ($pos === false) {
// write to file
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum