wildteen88 Posted September 23, 2007 Share Posted September 23, 2007 Whys it got newlines at the start of the script? You haven't modified the script have you, except the additions I told you to make? Change this part of the code to: // 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); } to: $i = 1; echo '<pre>'; // Change the array so that it's keyed on IP and the value is the hits foreach ($data as $d) { $d = trim($d); if(!empty($d)) { echo $i . ': ' . $d . "\n"; $i++; list($ip, $hits) = explode('|', $d); $new_data[$ip] = trim($hits); }else{$i++;} } echo '</pre>'; Re run the code. Quote Link to comment https://forums.phpfreaks.com/topic/69942-solved-wheres-the-error/page/2/#findComment-353347 Share on other sites More sharing options...
LemonInflux Posted September 23, 2007 Author Share Posted September 23, 2007 Wahay! That seems to have worked. I'll remove the echoes, then I'm done. Thanks a lot! Quote Link to comment https://forums.phpfreaks.com/topic/69942-solved-wheres-the-error/page/2/#findComment-353349 Share on other sites More sharing options...
LemonInflux Posted September 23, 2007 Author Share Posted September 23, 2007 Wait, one last problem; My body section now starts about half way down the page. Is there a way to move that up? Quote Link to comment https://forums.phpfreaks.com/topic/69942-solved-wheres-the-error/page/2/#findComment-353354 Share on other sites More sharing options...
wildteen88 Posted September 23, 2007 Share Posted September 23, 2007 What body section? I'm confused. Quote Link to comment https://forums.phpfreaks.com/topic/69942-solved-wheres-the-error/page/2/#findComment-353360 Share on other sites More sharing options...
LemonInflux Posted September 23, 2007 Author Share Posted September 23, 2007 Never mind, never closed the <pre>. OK, the hits counter worked, but the current content of the counter.php file is: <?php die('You are not allowed to view this page'); ?>| **.**.***.***|'number of hits' A) why does the php die have a | straight after? B) why can't multiple IPs be recorded? Quote Link to comment https://forums.phpfreaks.com/topic/69942-solved-wheres-the-error/page/2/#findComment-353365 Share on other sites More sharing options...
wildteen88 Posted September 23, 2007 Share Posted September 23, 2007 I dunno! My code doesn't add the line: <?php die('You are not allowed to view this page'); ?>| As for recording multiple ips it should do. Try hard coding a value for $_SERVER['REMOTE_ADDR'] variable, eg: add $_SERVER['REMOTE_ADDR'] = 'dummyip; at the top of the script. Run the script then remove that line re run script and you should see your ip address logged as well as your hard coded one. Quote Link to comment https://forums.phpfreaks.com/topic/69942-solved-wheres-the-error/page/2/#findComment-353371 Share on other sites More sharing options...
LemonInflux Posted September 23, 2007 Author Share Posted September 23, 2007 I added that line, so people can't see the IPs. Also, tried hardcoding the ip value, but all I got was the same line, <?php die('You are not allowed to view this page'); ?>| Quote Link to comment https://forums.phpfreaks.com/topic/69942-solved-wheres-the-error/page/2/#findComment-353374 Share on other sites More sharing options...
LemonInflux Posted September 23, 2007 Author Share Posted September 23, 2007 My code at the moment: <?php if(file_exists('counter.php')) { if(is_readable('counter.php') && is_writable('counter.php')) { // Open the file and read the details into an array $data = file('counter.php'); $i = 1; echo '<pre>'; // Change the array so that it's keyed on IP and the value is the hits foreach ($data as $d) { $d = trim($d); if(!empty($d)) { list($ip, $hits) = explode('|', $d); $new_data[$ip] = trim($hits); }else{$i++;} } // 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']]++; } // 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); } } echo '</pre>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/69942-solved-wheres-the-error/page/2/#findComment-353378 Share on other sites More sharing options...
wildteen88 Posted September 23, 2007 Share Posted September 23, 2007 You've removed the else statment from the if on this block of code: // 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']]++; } It should be: // 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; } Without the else newer IP's wont be added to current list. Only existing ips will be updated! <?php if(file_exists('counter.php')) { if(is_readable('counter.php') && is_writable('counter.php')) { // Open the file and read the details into an array $data = file('counter.php'); // remove first line from file, this is the PHP line to stop people from viewing contact.php array_shift($data); // Change the array so that it's keyed on IP and the value is the hits foreach ($data as $d) { $d = trim($d); if(!empty($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'); // shtop people from viewing the file. fwrite($handle, "<?php die('You are not allowed to view this page'); ?>\n"); foreach($new_data as $ip => $hits) { $data = $ip . '|' . $hits . "\n"; fwrite($handle, $data); } 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/page/2/#findComment-353380 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.