LemonInflux Posted September 19, 2007 Share Posted September 19, 2007 I have built a hit counter that logs the IPs of visitors to the site, but only once. Here's the code: <?PHP $ip = $_SERVER['REMOTE_ADDR']; $users = file("users.db.php"); $newuser = true; foreach($users as $userind) { $userinfo = explode("|", $userind); if($userinfo[0] == $ip) { $newuser = false; break; } } if($newuser) { $File = "counter.php"; $Handle = fopen($File, 'a'); $Data = $ip; fwrite($Handle, $Data); $Data = "|"; fwrite($Handle, $Data); $Data = "0\n"; fwrite($Handle, $Data); fclose($Handle); } ?> However, this doesn't record only once. This records every time they visit. Where's the error in this code? Quote Link to comment https://forums.phpfreaks.com/topic/69942-solved-wheres-the-error/ Share on other sites More sharing options...
Orio Posted September 19, 2007 Share Posted September 19, 2007 Can you show the file? Or at least a part of it? Orio Quote Link to comment https://forums.phpfreaks.com/topic/69942-solved-wheres-the-error/#findComment-351329 Share on other sites More sharing options...
LiamProductions Posted September 19, 2007 Share Posted September 19, 2007 make it search the DB for there IP then if it matches don't do anything and if it doesnt match any run the script to log. Quote Link to comment https://forums.phpfreaks.com/topic/69942-solved-wheres-the-error/#findComment-351338 Share on other sites More sharing options...
LemonInflux Posted September 20, 2007 Author Share Posted September 20, 2007 Sorry, only just checked this. A) That is the file, it's just a working project. B) The 'database' is a .php file. Didn't want to waste SQL room. Quote Link to comment https://forums.phpfreaks.com/topic/69942-solved-wheres-the-error/#findComment-351735 Share on other sites More sharing options...
MmmVomit Posted September 20, 2007 Share Posted September 20, 2007 I don't know what you mean by "waste sql room." A database is the ideal tool for this. Quote Link to comment https://forums.phpfreaks.com/topic/69942-solved-wheres-the-error/#findComment-351742 Share on other sites More sharing options...
sasa Posted September 20, 2007 Share Posted September 20, 2007 you read from file "users.db.php" and writw to file "counter.php" Quote Link to comment https://forums.phpfreaks.com/topic/69942-solved-wheres-the-error/#findComment-351757 Share on other sites More sharing options...
LemonInflux Posted September 20, 2007 Author Share Posted September 20, 2007 A) Phrased the SQL thing wrong. I meant I don't want to use SQL. Also, not sure why the users.db.php bit is in there o_O <?PHP $ip = $_SERVER['REMOTE_ADDR']; $users = file_get_contents("counter.php"); $userline = explode("\n", $users); $newuser = true; foreach($userline as $userind) { $userinfo = explode("|", $userind); if($userinfo[0] == $ip) { $newuser = false; break; } } if($newuser) { $Handle = fopen($users, 'a'); $Data = $ip; fwrite($Handle, $Data); $Data = "|"; fwrite($Handle, $Data); $Data = "0\n"; fwrite($Handle, $Data); fclose($Handle); } ?> New code. Although, still doesn't work. Now it's just not writing to the file. Quote Link to comment https://forums.phpfreaks.com/topic/69942-solved-wheres-the-error/#findComment-351764 Share on other sites More sharing options...
sasa Posted September 20, 2007 Share Posted September 20, 2007 $Handle = fopen('counter.php', 'a'); Quote Link to comment https://forums.phpfreaks.com/topic/69942-solved-wheres-the-error/#findComment-351779 Share on other sites More sharing options...
LemonInflux Posted September 21, 2007 Author Share Posted September 21, 2007 Yeah, I noticed that, but still no luck. :\ Won't record IPs Quote Link to comment https://forums.phpfreaks.com/topic/69942-solved-wheres-the-error/#findComment-352417 Share on other sites More sharing options...
LemonInflux Posted September 21, 2007 Author Share Posted September 21, 2007 bumpty. Quote Link to comment https://forums.phpfreaks.com/topic/69942-solved-wheres-the-error/#findComment-352436 Share on other sites More sharing options...
HuggieBear Posted September 21, 2007 Share Posted September 21, 2007 I'd go with something like this (untested)... <?php // Open the file and read the details into an array $data = file('counter.php'); // Change the array so that it's keyed on IP and the value is the hits foreach ($data as $d) { list($ip, $hits) = explode('|', $d); $new_data[$ip] = $hits; } // Check if the ip already exists if (in_array($_SERVER['REMOTE_ADDR'], array_keys($new_data))) { // If it does then increase the hits by one $new_data[$_SERVER['REMOTE_ADDR']]++; } else { // If it doesn't then add it $new_data[$_SERVER['REMOTE_ADDR']] = 1; } // Then here open your counter.php file and write it all back // REMEMBER the data you want is now in new_data, not the original $data ?> Regards Huggie Quote Link to comment https://forums.phpfreaks.com/topic/69942-solved-wheres-the-error/#findComment-352438 Share on other sites More sharing options...
HuggieBear Posted September 21, 2007 Share Posted September 21, 2007 If the file starts to get really big then you'll probably want to change it to use fopen() and fgets() rather than just file() or better still, consider a database. Regards Huggie Quote Link to comment https://forums.phpfreaks.com/topic/69942-solved-wheres-the-error/#findComment-352446 Share on other sites More sharing options...
LemonInflux Posted September 22, 2007 Author Share Posted September 22, 2007 no luck Quote Link to comment https://forums.phpfreaks.com/topic/69942-solved-wheres-the-error/#findComment-352966 Share on other sites More sharing options...
dewey_witt Posted September 22, 2007 Share Posted September 22, 2007 $newuser = return false; maybe that will work. Had to do that on a godaddy Windos server once. GODADDYS = PAIN IN THE A**! Quote Link to comment https://forums.phpfreaks.com/topic/69942-solved-wheres-the-error/#findComment-352973 Share on other sites More sharing options...
LemonInflux Posted September 22, 2007 Author Share Posted September 22, 2007 Blank Screen. Quote Link to comment https://forums.phpfreaks.com/topic/69942-solved-wheres-the-error/#findComment-352981 Share on other sites More sharing options...
wildteen88 Posted September 22, 2007 Share Posted September 22, 2007 Huggies code had a slight bug in it. Try the following code: <?php // Open the file and read the details into an array $data = file('counter.php'); // Change the array so that it's keyed on IP and the value is the hits foreach ($data as $d) { list($ip, $hits) = explode('|', $d); $new_data[$ip] = trim($hits); } // Check if the ip already exists if (isset($new_data) && array_key_exists($_SERVER['REMOTE_ADDR'], $new_data)) { // If it does then increase the hits by one $new_data[$_SERVER['REMOTE_ADDR']]++; } else { // If it doesn't then add it $new_data[$_SERVER['REMOTE_ADDR']] = 1; } // rewrite new data to counter.php $handle = fopen('counter.php', 'w'); foreach($new_data as $ip => $hits) { $data = $ip . '|' . $hits . "\n"; fwrite($handle, $data); } fclose($handle); ?>[code] [/code] Quote Link to comment https://forums.phpfreaks.com/topic/69942-solved-wheres-the-error/#findComment-353003 Share on other sites More sharing options...
LemonInflux Posted September 23, 2007 Author Share Posted September 23, 2007 I read through that, and I'm presuming that also records the number of times an IP visits? If so, it doesn't increase the value of 0. Apart from that, it's perfect. Quote Link to comment https://forums.phpfreaks.com/topic/69942-solved-wheres-the-error/#findComment-353263 Share on other sites More sharing options...
LemonInflux Posted September 23, 2007 Author Share Posted September 23, 2007 Bump. Quote Link to comment https://forums.phpfreaks.com/topic/69942-solved-wheres-the-error/#findComment-353312 Share on other sites More sharing options...
wildteen88 Posted September 23, 2007 Share Posted September 23, 2007 Code works flawlessly here. I'm assuming contact.php is formatted like the following: 127.0.0.1|1 192.168.2.2|1 The code should automatically increase the hits for the ip address already logged, eg if a user comes to the script and they have an ip address of 127.0.0.1 for example the script will increment the current hits for that ip address by 1. Which will result in contact.php changing to: 127.0.0.1|2 192.168.2.2|1 If a user has an ip address not currently listed in counter.php then it'll get added to the bottom, eg: 127.0.0.1|2 192.168.2.2|1 xxx.xx.x.xxx|1 xxx.xx.x.xxx being the ip address. Quote Link to comment https://forums.phpfreaks.com/topic/69942-solved-wheres-the-error/#findComment-353332 Share on other sites More sharing options...
LemonInflux Posted September 23, 2007 Author Share Posted September 23, 2007 Hm.. Quote Link to comment https://forums.phpfreaks.com/topic/69942-solved-wheres-the-error/#findComment-353335 Share on other sites More sharing options...
LemonInflux Posted September 23, 2007 Author Share Posted September 23, 2007 All that happens my end is the first visitor's IP is written to the counter, then no other IPs are written. On top of that, the number doesn't increase. Quote Link to comment https://forums.phpfreaks.com/topic/69942-solved-wheres-the-error/#findComment-353336 Share on other sites More sharing options...
wildteen88 Posted September 23, 2007 Share Posted September 23, 2007 Interesting, looks like something is not quite write. Added in some echo's at different stages of the script to see what is happening. Re run the following code: <?php echo 'PHP Version: ' . phpversion() . '<br />'; if(file_exists('counter.php')) { echo 'counter.php exists'; if(is_readable('counter.php') && is_writable('counter.php')) { echo ' and is readable and writable<br />'; echo 'Opening counter.php<br />'; // Open the file and read the details into an array $data = file('counter.php'); // Change the array so that it's keyed on IP and the value is the hits foreach ($data as $d) { list($ip, $hits) = explode('|', $d); $new_data[$ip] = trim($hits); } echo 'Retrived ips: <pre>' . print_r($new_data, true) . '</pre><br />'; // Check if the ip already exists if (isset($new_data) && array_key_exists($_SERVER['REMOTE_ADDR'], $new_data)) { // If it does then increase the hits by one $new_data[$_SERVER['REMOTE_ADDR']]++; echo $_SERVER['REMOTE_ADDR'] . ' exists hits incremented (' . $new_data[$_SERVER['REMOTE_ADDR']] . ')<br />'; } else { // If it doesn't then add it $new_data[$_SERVER['REMOTE_ADDR']] = 1; echo $_SERVER['REMOTE_ADDR'] . ' does not exists added to list:<pre>' . print_r($new_data, true) . '</pre>'; } echo 'Rewriting data to counter.php<br />'; // rewrite new data to counter.php $handle = fopen('counter.php', 'w'); foreach($new_data as $ip => $hits) { $data = $ip . '|' . $hits . "\n"; echo 'Adding: ' . $data . '<br />'; fwrite($handle, $data); } echo 'Counter.php closed'; fclose($handle); } else { echo 'counter.php is either not writable or readable!'; } } else { echo 'counter.php does not exists'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/69942-solved-wheres-the-error/#findComment-353341 Share on other sites More sharing options...
LemonInflux Posted September 23, 2007 Author Share Posted September 23, 2007 PHP Version: 4.4.4 counter.php exists and is readable and writable Opening counter.php Retrived ips: Array ( [ ] => [84.71.128.104] => 0 ) 84.71.128.104 exists hits incremented (1) Rewriting data to counter.php Adding: | Adding: 84.71.128.104|1 Counter.php closed very odd... Quote Link to comment https://forums.phpfreaks.com/topic/69942-solved-wheres-the-error/#findComment-353342 Share on other sites More sharing options...
wildteen88 Posted September 23, 2007 Share Posted September 23, 2007 Delete the contents of counter.php and re run the script again. Also run it a couple of times. Quote Link to comment https://forums.phpfreaks.com/topic/69942-solved-wheres-the-error/#findComment-353344 Share on other sites More sharing options...
LemonInflux Posted September 23, 2007 Author Share Posted September 23, 2007 Just did. I got this output: PHP Version: 4.4.4 counter.php exists and is readable and writable Opening counter.php Retrived ips: Array ( [ ] => [] => [**.**.***.***] => 8 ) **.**.***.*** exists hits incremented (9) Rewriting data to counter.php Adding: | Adding: | Adding: **.**.***.***|1 Counter.php closed Quote Link to comment https://forums.phpfreaks.com/topic/69942-solved-wheres-the-error/#findComment-353346 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.