Counter Frustrations
Alright, so I decide to write my own text counter using some tutorials I have found all over the net. One specfically that I found had the following code:
<?php
$counthandle=fopen("sample.php","r");
$getcurrent=fread($counthandle,filesize("sample.php"));
$getcurrent=$getcurrent+1;
fclose($counthandle);
$counthandle1=fopen("sample.php","w");
fputs($counthandle1,$getcurrent);
fclose($counthandle1);
$counthandle2=fopen("sample.php","r");
$getrecent=fread($counthandle2,filesize("sample.php"));
print "$getrecent";
fclose($counthandle2);
?>
It refers to a php file "sample" which is in the same directory as the html page that calls the above code. A few questions though:
-Does the above code go in the body of my html file? I have created a html file named "counter.html" and pasted the above code into it. Is this not the way it works?
- I created a "sample.php" file that has a single number in it initially ( a zero to start off). That was chMod to 666, will that work?
Whenever I upload the counter.html page and try and view it, I see a blank page. No nothing. I check the view source and the php appears there which if the php is executed correctly, it should not.
What am I doing wrong?
Suzanne posted this at 19:13 — 10th February 2004.
She has: 5,507 posts
Joined: Feb 2000
you can't parse php in a .html file -- name the page counter.php and try again? The php should NOT appear.
Greg K posted this at 19:21 — 10th February 2004.
He has: 2,145 posts
Joined: Nov 2003
The code you provided needs to go into a .PHP file, not an .HTML file.
PHP commands will not get processed unless they are in a file with an extention of .PHP (or another extension the server is set to procees, which usually doesn't include .html files)
Try renaming it to counter.php and then calling that page. Also, to avoid confusion later, I would use something like "sample.data", as the file (from the example you gave) does not actually contain php code, but just a numeric value. This may down the road help determine what files are what in the directory.
Just in case the page you got this code from didn't warn you, this type of counter, while very basic, will tend to be very inacurate. If it is on you home page, each time i go to the home page it increases (so if i go to a subpage, then come back to home, it registers another hit). Also someone could sit on the page and jsut keep hitting RELOAD/REFRESH to increase it.
IMO, if you seriously need a counter, it's worth to pay the small fee places like thecounter.com charge for a counter.
Hope this helps.
-Greg
mjs416 posted this at 19:28 — 10th February 2004.
They have: 127 posts
Joined: Dec 2003
Well, I managed to figure out the problem just as I checked back on the boards. It was the html/php extension problem.
I know the simple text counters are very inaccurate (the refresh/increment issue). I currently have a book on php that I am reading thru and just wanted to see some of the syntax in working order.
Thanks for the input all!!!!
Renegade posted this at 19:29 — 10th February 2004.
He has: 3,022 posts
Joined: Oct 2002
I made my own PHP counter code, if you want, you can use that or see how that works and try fixing your version of the code with it...
Mark Hensler posted this at 06:56 — 11th February 2004.
He has: 4,048 posts
Joined: Aug 2000
Just a passing thought...
I was wondering if running a short shell script would be more efficient than using two file handles for a text counter. The first handle to read the contents (required to increment the unknown number). The second to record the new number.
It just seemed like a waste to me that two handles should be required for a single task (incrementing an unknown number). Especially when we don't need the number (I'm assuming the number won't be displayed on the page).
So, I made a short shell script. I haven't benched it. It was just something to occupy 5 minutes of my time (spent writting this post).
cat sample.php | awk '{printf "%d\n", ($0+1);}' > sample.php
Mark Hensler
If there is no answer on Google, then there is no question.
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.