Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/125197-solved-flat-file-hit-counter/
Share on other sites

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.

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";
}
?>

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.

Solved :D

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?>");
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.