php help

biferi

Daemon Poster
Messages
690
I found a web site that shows you how to make a Hit Counter for a web pae.

Ijust want to know if this sounds right befor I start?

It tells me to take my index.html file witch is my Home Page and re name it index.php so I did this.

Then were ever I want my Hit Counter to be displayed I type this code

<? php
$filenam ="counter.txt;
$fd = fopen [filename,"r"];
$string = frad [$fd, filesize [$filename]];
echo "$string";
fclose[$fd];

$fd = fopen [$filname , "w"]
$fcounted = $fstring + 1 ;
$fout= fwrite [$ed , $fcounted] :
fclose[$ed] ;

?>

Then save it.

Then it tells me to open Notepade and save a blank text file called counter.txt.
And it tells me to save this to the same place.

What I want to know is all the php code it says to type did I type it right or do I need any Spces anywere???

It is hard to see if I should put a space anywere els.

I think I typed it right but I thought I would ask if anyone els did this before?
 
Sorry, but that's TERRIBLY written. Variable names are mis-spelled all the way through, the string literal isn't closed properly at the start, you're using square brackets instead of round ones for function calls, you're not always putting $ before your variable names, you're using variable names that you're not defining and you're defining variables that you never use...

I won't even go into the conventional problems such as calling variables "string"... If you want to code, even if you're just copying out code (something I detest) then you need to make sure you do it properly. Odd mis-spelling here or there might make sense to human readers, but throw it at a computer and it'll fail.

In fact, I'm pretty sure there's at least one thing wrong with every line of that code!

If you want to use PHP, my advice would be to go and look at some of the W3C tutorials, work your way through and then try and build up something like that once you've learnt a few more bits. Most of the errors you've made there will seem very obvious after an hour or so. When you've done that, if you still want a hit counter (they make a site look worse rather than better in my opinion) then you should be able to code one up yourself pretty easily.
 
Would this be it

$filename= "counter.txt" ;
$fd = fopen ($filename , "r") or die ("Can't open $filename") ;
$fstring = fread ($fd , filesize ($filename)) ;
echo "$fstring" ;
fclose($fd) ;

$fd = fopen ($filename , "w") or die ("Can't open $filename") ;
$fcounted = $fstring + 1 ;
$fout= fwrite ($fd , $fcounted ) ;
fclose($fd) ;
 
At a quick glance yes, that looks much more like it. Do you know how it works?
 
Well I know that everytime my page is visited the PHP file ads a 1. to the counter.txt file.

But I do not understand every line can you mabe tell me what eatch limne meens??
 
Code:
$filename= "counter.txt";
Firstly we declare the filename variable to hold the location of the text file. This is pretty self explanatory.

Code:
$fd = fopen ($filename , "r") or die ("Can't open $filename") ;
Next, try to open the file declared above for read only access (that's what the r is for) or throw an error if it can't be opened.

Code:
$fstring = fread ($fd , filesize ($filename)) ;
Read the entire contents of the file (a number hopefully!) and store it in $fstring

Code:
echo "$fstring" ;
Print out contents of the file.

Code:
fclose($fd) ;
Close the file for reading.

Code:
$fd = fopen ($filename , "w") or die ("Can't open $filename") ;
Open the file for writing or throw an error if it can't be opened.

Code:
$fcounted = $fstring + 1 ;
Declare a variable that's $fstring (the contents of the file previously) by one

Code:
$fout= fwrite ($fd , $fcounted ) ;
Write the new number to the file

Code:
fclose($fd) ;
Close the file for writing.

Make more sense now? :)
 
I need some help still.

Like I say the web site told me to put all my PHP Code in my index.html file then re name it index.php

But when I do that the browser wont open it.

what am I not understanding????
 
Is PHP enabled on your server?

Create a file called whatever.php, put:
Code:
<?php phpinfo(); ?>
...in it and see what the browser outputs.

For what it's worth, why do you want a hit counter? They generally make a site look worse rather than better, and are generally classed as antiquated things best left in the 90's... how many professional sites out there do you see with hit counters these days?

If you want site statistics, then you're far better off using an appropriate stats package.
 
What is a stats package.???????????????

And just tell me one thing

The web site told me to put the php code in my index page but rename it index.php

If it is not called index.html the browser wont go to it right???
 
Google analytics is the choice of many. AWStats is another open source one you might want to look at.

If it is not called index.html the browser wont go to it right???
It's nothing to do with what your browser goes to, it's all to do with your server settings... if it's set up properly then it'll go to index.php no problem. If it's not set up properly or PHP isn't installed then it may well not!

What server are you using to run your site?
 
Back
Top Bottom