|
Posted in Computers & Tech / Programming / Scripting / PHP
Author: ChronicLoser Total-Replies: 4 alright here's what I want to do: I made a counter to count unique visitors without a mysql. But the problem is that the '.dat' file I'm writing on (unique.dat) tends to become too big when I have too many visitors. Thus, I tried to add an if/then statement. Kay, now here's the problem: even though this code successfully runs, it's not a hundred-percent accurate. Sometimes it rewrites a user's IP even though it is already on the '.dat' file. Even though it doesn't actually affect the $hits, I'd still like to know what I did wrong. CODE<?php$filename = "unique.dat"; $file = file($filename); $file = array_unique($file); $hits = count($file); $remote = $_SERVER["REMOTE_ADDR"]; $var = $_SERVER['PHP_SELF']; $var = 'unique.dat'; $wholefile = file_get_contents($var); preg_match_all("|$remote|U",$wholefile, $out, PREG_PATTERN_ORDER); if(!($out[0][0] == $remote)) { $fd = fopen ($filename , "a+"); $fout = fwrite ($fd , "$remote\n"); fclose($fd); echo "bad"; } echo $hits; ?> hope someone could help me out here ^_^
Wed Feb 9, 2005
Reply New Discussion
Posted in Computers & Tech / Designing
Author: 8ennett Total-Replies: 18 There are many distinguishing features of a hit counter, some stay plain and simple and increment every time a person visits a particular page regardless of any other factors. Other are slightly more advanced and increment only if the person visiting has an IP that hasn't been recorded before, and others go even further and track users who have visited before using cookies and other methods to prevent a unique user count from incrementing if the same person has already visited (even with a different ip), and then you get the ones that use all kinds of tracking information and so on and pull all kinds of data from the user to be used for nefarious purposes mwahahahaha lol For a simple hit counter that increments only when a new IP visits the site then get a hosting account with a mysql database (like astahost for instance), create the following table: CODECREATE TABLE `hitcount` {`id` int(255) NOT NULL auto_increment, `ip` int(15) NOT NULL, PRIMARY `id` } The on your home page where you want to place the counter insert the following php script: CODE<?php$openhitcount = mysql_connect('Your database connection details go here'); mysql_select_db('Your database name goes here'); if (mysql_num_rows(mysql_query("SELECT id FROM hitcount WHERE ip=".mysql_real_escape_string($_SESSION['server']['REMOTE_HOST']))) == 0){ mysql_query("INSERT INTO hitcount (ip) VALUES (".mysql_real_escape_string($_SESSION['server']['REMOTE_HOST']).")"); } echo "We've had ".number_format(mysql_num_rows(mysql_query("SELECT id FROM hitcount")))." visitors"; mysql_close($openhitcount); ?> or something similar to that anyway, I've had a lot to drink so my php functions may not be spelled correctly or my code format may be out of whack, either way that's a very simple, very secure and very effective hit counter...I think...lol
Mon Aug 29, 2011
Reply New Discussion
|