avatar.alex Posted May 1, 2007 Share Posted May 1, 2007 ok I don't want the page counter to recount the page each time me or anyone else roloads it or goes it it I just want one count per person could anyone help me out heres the code thank you: <?php //-----------------------------------------------// //---------------Simple Logger-------------------// //-----------------------------------------------// // This code is (c) to TheSavior (Eli White) as // // of 2005-2006. This code may NOT be reproduced // // ,or // // distributed by any means, unless you have // // explicit written permissions by TheSavior // //-----------------------------------------------// // You may edit this file as you wish, as long // // as it isn't redistributed after words. // // Free support is provided for all scripts on // // http://www.thesavior.co.nr // //-----------------------------------------------// // I hope you enjoy this script, now get to the // // fun part of looking at how it works! Its a // // joy. XD // //-----------------------------------------------// //---------------------------------------------- // Log files with permissions to write //---------------------------------------------- $log = "logger.txt"; $file = "users.txt"; //---------------------------------------------- // Get our variables for future use //---------------------------------------------- $today = getdate(); $month = $today[month]; $mday = $today[mday]; $year = $today[year]; $date = $mday . $month . $year; $remote = $_SERVER["REMOTE_ADDR"]; $timeoutsecs = 900; $timestamp = time(); $timeout = ($timestamp-$timeoutsecs); $online = fopen("$file", "a+"); $write = $remote."||".$timestamp."n"; //---------------------------------------------- // Write to our logfile //---------------------------------------------- $fp = fopen($log, "a"); $line = "|" . $mday . $month . $year . "\n"; $size = strlen($line); fputs($fp, $line, $size); fclose($fp); $contents = file($log); $total_hits = count($contents); //---------------------------------------------- // Formmating for daily_hits_size //---------------------------------------------- $daily_hits = array(); for ($i=0;$i<count($contents);$i++) { $entry = explode("|", $contents[$i]); if ($date == rtrim($entry[1])) { array_push($daily_hits, $entry[0]); } } $daily_hits_size = count($daily_hits); //---------------------------------------------- // Users Online //---------------------------------------------- fwrite($online, $write); fclose($online); $online_array = array(); $file_array = file($file); foreach($file_array as $newdata) { list($ip, $time) = explode("||", $newdata); if($time >= $timeout){ array_push($online_array, $ip); } } $online_array = array_unique($online_array); $online = 1+ count($online_array); //---------------------------------------------- // Echo our stats to the page //---------------------------------------------- echo "<span class=\"stats\">Page hits:<strong>" . $total_hits . "</strong><br /> Todays Page hits: <strong>" . $daily_hits_size . "</strong><br /> Users Online: <strong>" . $online . "</strong><br /></span> " ?> Quote Link to comment https://forums.phpfreaks.com/topic/49390-hit-counter-help/ Share on other sites More sharing options...
nikkieijpen Posted May 1, 2007 Share Posted May 1, 2007 Two ways: 1. you can count unique visits by IP addresses. If an IP address is already logged, don't log it. 2. use sessions. If a visit is being logged, set a session variable to 'logged' (or whatever you like). Don't log a visit when the session has that value. Quote Link to comment https://forums.phpfreaks.com/topic/49390-hit-counter-help/#findComment-242175 Share on other sites More sharing options...
avatar.alex Posted May 1, 2007 Author Share Posted May 1, 2007 where or how would I add that into the script Quote Link to comment https://forums.phpfreaks.com/topic/49390-hit-counter-help/#findComment-242701 Share on other sites More sharing options...
clown[NOR] Posted May 1, 2007 Share Posted May 1, 2007 i just finished a project like that earlier today... this logs the IP's visiting your site <?php function countIP() { $ip = $_SERVER['REMOTE_ADDR']; $chkIP = checkIP($ip); if (!$chkIP) { $write = $ip."\n"; $fh = fopen('filename.txt', 'a+'); fwrite($fh, $write); fclose($fh); return calcIP(); } else { return calcIP(); } } function checkIP($ip) { $lines = file('filename.txt'); foreach ($lines as $line) { if (trim($line) == $ip) return true; } } function calcIP() { $lines = file('filename.txt'); $i = 1; foreach ($lines as $line) { $i++; } return $i-1; } ?> edit it to your needs and wherever you want the counter just add this <?php echo countIP(); ?> EDIT: WTF happened to the code?! Quote Link to comment https://forums.phpfreaks.com/topic/49390-hit-counter-help/#findComment-242713 Share on other sites More sharing options...
avatar.alex Posted May 1, 2007 Author Share Posted May 1, 2007 thanks what functions would I use if I just wanted to count how many people were on today Quote Link to comment https://forums.phpfreaks.com/topic/49390-hit-counter-help/#findComment-242732 Share on other sites More sharing options...
clown[NOR] Posted May 1, 2007 Share Posted May 1, 2007 there's 2 ways to do that... 1. Make a new file for every day. 2. When today is over you clear the file and start over.. with option 1 you can keep track of visits and make visitor statistics with option 2 you dont end up having hundreds of files Quote Link to comment https://forums.phpfreaks.com/topic/49390-hit-counter-help/#findComment-242740 Share on other sites More sharing options...
avatar.alex Posted May 2, 2007 Author Share Posted May 2, 2007 thats cool well i think i got it but how would I add it so the it logs the ip address... / // Log files with permissions to write $log = "logger.txt"; $file = "users.txt"; // Get our variables for future use $today = getdate(); $month = $today[month]; $mday = $today[mday]; $year = $today[year]; $date = $mday . $month . $year; $remote = $_SERVER["REMOTE_ADDR"]; $timeoutsecs = 900; $timestamp = time(); $timeout = ($timestamp-$timeoutsecs); $online = fopen("$file", "a+"); $write = $remote."||".$timestamp."n"; // Write to our logfile $fp = fopen($log, "a"); $line = "|" . $mday . $month . $year . "\n"; $size = strlen($line); fputs($fp, $line, $size); fclose($fp); $contents = file($log); $total_hits = count($contents); // Formmating for daily_hits_size $daily_hits = array(); for ($i=0;$i<count($contents);$i++) { $entry = explode("|", $contents[$i]); if ($date == rtrim($entry[1])) { array_push($daily_hits, $entry[0]); } } $daily_hits_size = count($daily_hits); Quote Link to comment https://forums.phpfreaks.com/topic/49390-hit-counter-help/#findComment-243009 Share on other sites More sharing options...
clown[NOR] Posted May 2, 2007 Share Posted May 2, 2007 in the script you're using.. the users IP address is stored into $remote ... just write the value of $remote to a text file Quote Link to comment https://forums.phpfreaks.com/topic/49390-hit-counter-help/#findComment-243017 Share on other sites More sharing options...
avatar.alex Posted May 2, 2007 Author Share Posted May 2, 2007 Im looking through these two scripts and trying to put them together but im kinda stuck on where to put the parts....I think that I would take the function checkIP($ip) and change the $ip it $log but im not sure? <? function countIP() { $ip = $_SERVER['REMOTE_ADDR']; $chkIP = checkIP($ip); if (!$chkIP) { $write = $ip."\n"; $fh = fopen('filename.txt', 'a+'); fwrite($fh, $write); fclose($fh); return calcIP(); } else { return calcIP(); } } function checkIP($ip) { // Log files with permissions to write $log = "logger.txt"; $file = "users.txt"; // Get our variables for future use $today = getdate(); $month = $today[month]; $mday = $today[mday]; $year = $today[year]; $date = $mday . $month . $year; $remote = $_SERVER["REMOTE_ADDR"]; $timeoutsecs = 900; $timestamp = time(); $timeout = ($timestamp-$timeoutsecs); $online = fopen("$file", "a+"); $write = $remote."||".$timestamp."n"; // Write to our logfile $fp = fopen($log, "a"); $line = "|" . $mday . $month . $year . "\n"; $size = strlen($line); fputs($fp, $line, $size); fclose($fp); $contents = file($log); $total_hits = count($contents); // Formmating for daily_hits_size $daily_hits = array(); for ($i=0;$i<count($contents);$i++) { $entry = explode("|", $contents[$i]); if ($date == rtrim($entry[1])) { array_push($daily_hits, $entry[0]); } } $daily_hits_size = count($daily_hits); Quote Link to comment https://forums.phpfreaks.com/topic/49390-hit-counter-help/#findComment-243021 Share on other sites More sharing options...
clown[NOR] Posted May 2, 2007 Share Posted May 2, 2007 no... $ip in the checkIP function is storing the users IP address so it can look for it in the textfile the IP's are saved to Quote Link to comment https://forums.phpfreaks.com/topic/49390-hit-counter-help/#findComment-243024 Share on other sites More sharing options...
avatar.alex Posted May 2, 2007 Author Share Posted May 2, 2007 ok let me try again sorry kinda sorta new to the whole php thing lol Quote Link to comment https://forums.phpfreaks.com/topic/49390-hit-counter-help/#findComment-243029 Share on other sites More sharing options...
clown[NOR] Posted May 2, 2007 Share Posted May 2, 2007 but it looks like the script you have allready stores the IPs... so there's no need for you to try make my script and the one you allready have work together Quote Link to comment https://forums.phpfreaks.com/topic/49390-hit-counter-help/#findComment-243031 Share on other sites More sharing options...
avatar.alex Posted May 2, 2007 Author Share Posted May 2, 2007 no but when ever I refresh the page it adds one to the counter so I just want it so it only counts one person at a time not so it counts everytime I refreash the page. Quote Link to comment https://forums.phpfreaks.com/topic/49390-hit-counter-help/#findComment-243042 Share on other sites More sharing options...
clown[NOR] Posted May 2, 2007 Share Posted May 2, 2007 well.. you have to add something like this $chkIP = checkIP($remote); before the section where the users IP address get's written to the file then when the script is about to write the IP you run an IF statement to see if the IP was found or not. if (!$chkIP) { make the script add the IP } Quote Link to comment https://forums.phpfreaks.com/topic/49390-hit-counter-help/#findComment-243047 Share on other sites More sharing options...
avatar.alex Posted May 2, 2007 Author Share Posted May 2, 2007 ok I did it right I think but it says that there is a error on line 45 which is the line with the $chkIP = checkIP($remote); $log = "logger.txt"; $file = "users.txt"; // Get our variables for future use $today = getdate(); $month = $today[month]; $mday = $today[mday]; $year = $today[year]; $date = $mday . $month . $year; $remote = $_SERVER["REMOTE_ADDR"]; $timeoutsecs = 900; $timestamp = time(); $timeout = ($timestamp-$timeoutsecs); $online = fopen("$file", "a+"); $write = $remote."||".$timestamp."n"; // Write to our logfile $chkIP = checkIP($remote); if (!$chkIP) { $fp = fopen($log, "a"); $line = "|" . $mday . $month . $year . "\n"; $size = strlen($line); fputs($fp, $line, $size); fclose($fp); $contents = file($log); $total_hits = count($contents); } // Formmating for daily_hits_size $daily_hits = array(); for ($i=0;$i<count($contents);$i++) { $entry = explode("|", $contents[$i]); if ($date == rtrim($entry[1])) { array_push($daily_hits, $entry[0]); } Quote Link to comment https://forums.phpfreaks.com/topic/49390-hit-counter-help/#findComment-243059 Share on other sites More sharing options...
john010117 Posted May 2, 2007 Share Posted May 2, 2007 What exactly is the error message? Quote Link to comment https://forums.phpfreaks.com/topic/49390-hit-counter-help/#findComment-243062 Share on other sites More sharing options...
avatar.alex Posted May 2, 2007 Author Share Posted May 2, 2007 Fatal error: Call to undefined function: checkip() in /home/mystrh00/domains/witchin.net/public_html/blog/logger.php on line 45 Quote Link to comment https://forums.phpfreaks.com/topic/49390-hit-counter-help/#findComment-243065 Share on other sites More sharing options...
avatar.alex Posted May 2, 2007 Author Share Posted May 2, 2007 does anyone know why the error is like that Quote Link to comment https://forums.phpfreaks.com/topic/49390-hit-counter-help/#findComment-243508 Share on other sites More sharing options...
clown[NOR] Posted May 2, 2007 Share Posted May 2, 2007 it cant find the function checkIP() Quote Link to comment https://forums.phpfreaks.com/topic/49390-hit-counter-help/#findComment-243551 Share on other sites More sharing options...
avatar.alex Posted May 2, 2007 Author Share Posted May 2, 2007 did I do something wrong Quote Link to comment https://forums.phpfreaks.com/topic/49390-hit-counter-help/#findComment-243561 Share on other sites More sharing options...
clown[NOR] Posted May 2, 2007 Share Posted May 2, 2007 well.. since you're getting an error message i would belive you did but can you post the whole code? Quote Link to comment https://forums.phpfreaks.com/topic/49390-hit-counter-help/#findComment-243570 Share on other sites More sharing options...
avatar.alex Posted May 2, 2007 Author Share Posted May 2, 2007 <?php //-----------------------------------------------// //---------------Simple Logger-------------------// //-----------------------------------------------// // This code is (c) to TheSavior (Eli White) as // // of 2005-2006. This code may NOT be reproduced // // ,or // // distributed by any means, unless you have // // explicit written permissions by TheSavior // //-----------------------------------------------// // You may edit this file as you wish, as long // // as it isn't redistributed after words. // // Free support is provided for all scripts on // // http://www.thesavior.co.nr // //-----------------------------------------------// // I hope you enjoy this script, now get to the // // fun part of looking at how it works! Its a // // joy. XD // //-----------------------------------------------// //---------------------------------------------- // Log files with permissions to write //---------------------------------------------- $log = "logger.txt"; $file = "users.txt"; //---------------------------------------------- // Get our variables for future use //---------------------------------------------- $today = getdate(); $month = $today[month]; $mday = $today[mday]; $year = $today[year]; $date = $mday . $month . $year; $remote = $_SERVER["REMOTE_ADDR"]; $timeoutsecs = 900; $timestamp = time(); $timeout = ($timestamp-$timeoutsecs); $online = fopen("$file", "a+"); $write = $remote."||".$timestamp."n"; //---------------------------------------------- // Write to our logfile //---------------------------------------------- $chkIP = checkIP($remote); if (!$chkIP) { $fp = fopen($log, "a"); $line = "|" . $mday . $month . $year . "\n"; $size = strlen($line); fputs($fp, $line, $size); fclose($fp); $contents = file($log); $total_hits = count($contents); } //---------------------------------------------- // Formmating for daily_hits_size //---------------------------------------------- $daily_hits = array(); for ($i=0;$i<count($contents);$i++) { $entry = explode("|", $contents[$i]); if ($date == rtrim($entry[1])) { array_push($daily_hits, $entry[0]); } } $daily_hits_size = count($daily_hits); //---------------------------------------------- // Users Online //---------------------------------------------- fwrite($online, $write); fclose($online); $online_array = array(); $file_array = file($file); foreach($file_array as $newdata) { list($ip, $time) = explode("||", $newdata); if($time >= $timeout){ array_push($online_array, $ip); } } $online_array = array_unique($online_array); $online = 1+ count($online_array); //---------------------------------------------- // Echo our stats to the page //---------------------------------------------- echo "<span class=\"stats\">Page hits:<strong>" . $total_hits . "</strong><br /> Todays Page hits: <strong>" . $daily_hits_size . "</strong><br /> Users Online: <strong>" . $online . "</strong><br /></span> " ?> Quote Link to comment https://forums.phpfreaks.com/topic/49390-hit-counter-help/#findComment-243696 Share on other sites More sharing options...
dj-kenpo Posted May 2, 2007 Share Posted May 2, 2007 it wants the function checkIP. you don't seem to have that function there, hence this is incomplete code (unless I'm blind. which I sometimes am). can anyone comment, is storing hits in a textfile MUCH slower than an sql database? I would assume by alot! also, keep in mind, people at offices/colleges many times use shared IP. therefore, if 30 people in an office look at your site and you filter by IP, you will get an very, very incorrect count. it's better to use sessions. Quote Link to comment https://forums.phpfreaks.com/topic/49390-hit-counter-help/#findComment-243761 Share on other sites More sharing options...
avatar.alex Posted May 2, 2007 Author Share Posted May 2, 2007 ok so how would I use sessions in this one Quote Link to comment https://forums.phpfreaks.com/topic/49390-hit-counter-help/#findComment-243765 Share on other sites More sharing options...
clown[NOR] Posted May 2, 2007 Share Posted May 2, 2007 you cant use the checkIP() function I gave you unless you add the function into the script... so just copy, paste and modify it and it should be working Quote Link to comment https://forums.phpfreaks.com/topic/49390-hit-counter-help/#findComment-243769 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.