View Full Version : Recording IP addresses with PHP??

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:

$CountFile = "folder_name/counter.txt";
$Count = file($CountFile);
$Count = implode("", $Count);
$counter = true;
$OpenFile = fopen($CountFile, "r+");
$user_ip = GetHostByName($_SERVER['REMOTE_ADDR']);
$banned = array('', '');
if (!in_array($user_ip, $banned)) {
fwrite($OpenFile, $Count);

// 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");

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

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'])) {
} 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