Page 1 of 2

[SOLVED] creating a simple hit counter

Posted: Thu Jun 24, 2004 10:11 am
by andylyon87

Code: Select all

function read(){
         $TheFile = "php/form_data/hits.txt";
         $data = file($TheFile);
         $Open = fopen ($TheFile, "w+");
         $getline = explode("\t",$dataї$n]);
         if($Open){
for($n = 0; $n<count($data); $n++)&#123;
         print("$getline&#1111;0]");
         $ad = $getline&#1111;0];
         $add = $ad + 1;
         fwrite ($Open,$add);
         fclose($Open);
         &#125;&#125;&#125;
the above is the code for the hit counter, it is supposed to open hits.txt ($TheFile) and add1 to it and then close the file. However as it stands it doesn't, it replaces the number in the file with a space. I have a feeling its to do with the way it is reading the file so can anyone help cause its the first time I've tried to do something like this so I'm just guessin really.

Posted: Thu Jun 24, 2004 10:19 am
by Grim...
Is the text file set to CHMOD 777?

If that doesn't help, you could try this instead:

Code: Select all

<?php
function counter() {
$file=file("counter.txt");
echo ("[$file[0]]");
$fp=fopen("counter.txt","w");
fwrite($fp,$file[0]+1);
fclose($fp);
}
?>

Posted: Thu Jun 24, 2004 10:24 am
by launchcode
The following code will work for a simple text file based hit counter, it has a few additional checks - you need to ensure a blank text file (called counter.txt) exists in the same location.

Code: Select all

<?php
	//	Hit Counter
	$counter_file = 'counter.txt';
	
	if (file_exists($counter_file))
	{
		$counter = file_get_contents($counter_file);
		if (is_numeric($counter))
		{
			$counter++;
		}
		else
		{
			$counter = 0;
		}
		$fp = fopen($counter_file, 'w');
		fwrite($fp, $counter);
		fclose($fp);
	}

	echo $counter;
?>

Posted: Thu Jun 24, 2004 10:25 am
by andylyon87
will try code and yes the chmod is set correct :)
cheers

Posted: Thu Jun 24, 2004 10:34 am
by andylyon87
1st one doesnt work just shows the number in the file

The second has an error, file_get_contents is undefined

Posted: Thu Jun 24, 2004 10:46 am
by launchcode
You have an installation of PHP dating back to the dark ages.

Posted: Thu Jun 24, 2004 10:53 am
by Grim...
andylyon87 wrote:1st one doesnt work just shows the number in the file
Er, what did you expect a hit counter to do?

Posted: Thu Jun 24, 2004 10:54 am
by andylyon87
??? wats that meant to mean
accordin to php.net
Description
string file_get_contents ( string filename [, bool use_include_path [, resource context]])

Posted: Thu Jun 24, 2004 10:55 am
by andylyon87
Er, what did you expect a hit counter to do?
lol, v funy, it shows the sae number every time you refresh

Posted: Thu Jun 24, 2004 10:56 am
by launchcode
Exactly - the command is correct and there are no syntax errors in the code I posted, which means if it gives you an "undefined" error your version of PHP doesn't have support for that function. I.e. it's way old.

Posted: Thu Jun 24, 2004 10:57 am
by Grim...
I'm inclined to agree with LaunchCode - check your version of PHP.

Posted: Thu Jun 24, 2004 11:00 am
by andylyon87
I use easy php 1.6, I'll try it on my server

Posted: Thu Jun 24, 2004 11:00 am
by Grim...
I've just checked the code I posted on V4.3.6 and it works fine.

Use the following code

Code: Select all

<?php phpinfo(); ?>
to check your version.

Posted: Thu Jun 24, 2004 11:01 am
by andylyon87
oh an that supports php 4

Posted: Thu Jun 24, 2004 11:03 am
by andylyon87
It 4.2.0 thats not that old