DeanWhitehouse Posted September 21, 2008 Share Posted September 21, 2008 Ok, i havent made a flat file system before so i could use some help. What i need to do is count how many hits the site pages get, each page seperatly would be good, and then i will need there IP and there refferer. I know how to do all this db style, the start to the code would be <?php $ip = $_SERVER['REMOTE_ADDR']; $hits = //this would be the current hits the page has $hits++; $refferer = $_SERVER['HTTP_REFERER']; ?> Any help please. Quote Link to comment https://forums.phpfreaks.com/topic/125197-solved-flat-file-hit-counter/ Share on other sites More sharing options...
.josh Posted September 21, 2008 Share Posted September 21, 2008 fopen, fwrite, fclose. Or google flatfile hit counters,as there are a million and one out there. Quote Link to comment https://forums.phpfreaks.com/topic/125197-solved-flat-file-hit-counter/#findComment-647133 Share on other sites More sharing options...
DeanWhitehouse Posted September 21, 2008 Author Share Posted September 21, 2008 Using f(open,write,close), using them dont i have to write all the contents each time, instead of just updating one? Quote Link to comment https://forums.phpfreaks.com/topic/125197-solved-flat-file-hit-counter/#findComment-647142 Share on other sites More sharing options...
.josh Posted September 21, 2008 Share Posted September 21, 2008 What'd you expect out of a flatfile? That's why databases are better. Quote Link to comment https://forums.phpfreaks.com/topic/125197-solved-flat-file-hit-counter/#findComment-647152 Share on other sites More sharing options...
DeanWhitehouse Posted September 21, 2008 Author Share Posted September 21, 2008 Calm down, only asking a question. Does anyone know if there is a more effiecent way, using flat file. Quote Link to comment https://forums.phpfreaks.com/topic/125197-solved-flat-file-hit-counter/#findComment-647158 Share on other sites More sharing options...
Mchl Posted September 21, 2008 Share Posted September 21, 2008 ... sqlite? Quote Link to comment https://forums.phpfreaks.com/topic/125197-solved-flat-file-hit-counter/#findComment-647161 Share on other sites More sharing options...
.josh Posted September 21, 2008 Share Posted September 21, 2008 No worries, I'm not wiggin'.... You can use file() to grab all the contents of the file and put each line in an array. You can then do some regex or depending on how each line is setup, further split each line into an array. Change the number, glue it all back together, and save the whole thing. I suppose you can look into using xml... But seriously, there is no "better" way to do it, because with a flatfile, it's all just bytes to the computer. How the data is structured is entirely up to you. So there's really no way to single out a single piece of data, change it, and then save only that part of the file. Each time you save it, it would be saving the file as a whole. Quote Link to comment https://forums.phpfreaks.com/topic/125197-solved-flat-file-hit-counter/#findComment-647163 Share on other sites More sharing options...
DeanWhitehouse Posted September 21, 2008 Author Share Posted September 21, 2008 Ok, i have tried 4 ways of getting the data from the file and only the last one did. <?php $config_file = "hits.php"; $fw = fopen($config_file,"r") or die("Problem opening log"); file_get_contents($config_file); file($config_file); include($config_file); //these are the four ways, the file just has 2 vars in it with data stored in them. But only using include , was the var present in my below code. if(isset($test)) { echo "present"; } else { echo "not"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/125197-solved-flat-file-hit-counter/#findComment-647168 Share on other sites More sharing options...
.josh Posted September 21, 2008 Share Posted September 21, 2008 That's because with the first 3, $test = 'blah'; doesn't mean anything. It's all just random data. It's like doing this: $x = "$test = 'blah'"; and then expecting to echo $test. Include() will actually parse what's in the file. There are examples in the manual that show you how to open a file, write something to it, and close it. Quote Link to comment https://forums.phpfreaks.com/topic/125197-solved-flat-file-hit-counter/#findComment-647170 Share on other sites More sharing options...
DeanWhitehouse Posted September 21, 2008 Author Share Posted September 21, 2008 Solved this is my code <?php $config_file = "hits.php"; include($config_file); $home_page = $home_page; $wine_page = $wine_page; $menu = $menu; $recipes = $recipes; $about = $about; $functions = $functions; $contact = $contact; $hits = $hits; $hits .= "IP: ".$_SERVER['REMOTE_ADDR']." Site: ".$_SERVER['HTTP_REFERER']."<br>"; $fw = fopen($config_file,"w+"); $home_page = "\$home_page = \"".$home_page."\";\n"; $wine_page = "\$wine_page = \"".$wine_page."\";\n"; $menu = "\$menu = \"".$menu."\";\n"; $recipes = "\$recipes = \"".$recipes."\";\n"; $about = "\$hits = \"".$about."\";\n"; $functions = "\$hits = \"".$functions."\";\n"; $contact = "\$hits = \"".$contact."\";\n"; $hits = "\$hits = \"".$hits."\";\n"; $config_write = $home_page.$wine_page.$menu.$recipes.$about.$functions.$contact.$hits; fwrite($fw, "<?php\n".$config_write."\n?>"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/125197-solved-flat-file-hit-counter/#findComment-647184 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.