[SOLVED] creating a simple hit counter

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

andylyon87
Forum Contributor
Posts: 168
Joined: Sat Jan 31, 2004 5:31 am
Location: Dundee

[SOLVED] creating a simple hit counter

Post 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.
Last edited by andylyon87 on Thu Jun 24, 2004 11:38 am, edited 1 time in total.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post 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);
}
?>
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post 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;
?>
andylyon87
Forum Contributor
Posts: 168
Joined: Sat Jan 31, 2004 5:31 am
Location: Dundee

Post by andylyon87 »

will try code and yes the chmod is set correct :)
cheers
andylyon87
Forum Contributor
Posts: 168
Joined: Sat Jan 31, 2004 5:31 am
Location: Dundee

Post by andylyon87 »

1st one doesnt work just shows the number in the file

The second has an error, file_get_contents is undefined
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post by launchcode »

You have an installation of PHP dating back to the dark ages.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post 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?
andylyon87
Forum Contributor
Posts: 168
Joined: Sat Jan 31, 2004 5:31 am
Location: Dundee

Post by andylyon87 »

??? wats that meant to mean
accordin to php.net
Description
string file_get_contents ( string filename [, bool use_include_path [, resource context]])
andylyon87
Forum Contributor
Posts: 168
Joined: Sat Jan 31, 2004 5:31 am
Location: Dundee

Post by andylyon87 »

Er, what did you expect a hit counter to do?
lol, v funy, it shows the sae number every time you refresh
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post 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.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

I'm inclined to agree with LaunchCode - check your version of PHP.
andylyon87
Forum Contributor
Posts: 168
Joined: Sat Jan 31, 2004 5:31 am
Location: Dundee

Post by andylyon87 »

I use easy php 1.6, I'll try it on my server
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post 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.
andylyon87
Forum Contributor
Posts: 168
Joined: Sat Jan 31, 2004 5:31 am
Location: Dundee

Post by andylyon87 »

oh an that supports php 4
andylyon87
Forum Contributor
Posts: 168
Joined: Sat Jan 31, 2004 5:31 am
Location: Dundee

Post by andylyon87 »

It 4.2.0 thats not that old
Post Reply