Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 11-01-2009, 03:41 PM   #1
Daemon Poster
 
Join Date: May 2009
Posts: 690
Smile php help

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?
__________________

biferi is offline   Reply With Quote
Old 11-01-2009, 05:42 PM   #2
Site Team
 
berry120's Avatar
 
Join Date: Jul 2009
Location: England, UK
Posts: 3,425
Default Re: php help

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.
__________________

berry120 is offline   Reply With Quote
Old 11-01-2009, 08:50 PM   #3
Daemon Poster
 
Join Date: May 2009
Posts: 690
Smile Re: php help

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) ;
biferi is offline   Reply With Quote
Old 11-02-2009, 04:31 AM   #4
Site Team
 
berry120's Avatar
 
Join Date: Jul 2009
Location: England, UK
Posts: 3,425
Default Re: php help

At a quick glance yes, that looks much more like it. Do you know how it works?
berry120 is offline   Reply With Quote
Old 11-02-2009, 11:46 AM   #5
Daemon Poster
 
Join Date: May 2009
Posts: 690
Default Re: php help

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??
biferi is offline   Reply With Quote
Old 11-02-2009, 12:16 PM   #6
Site Team
 
berry120's Avatar
 
Join Date: Jul 2009
Location: England, UK
Posts: 3,425
Default Re: php help

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?
berry120 is offline   Reply With Quote
Old 11-02-2009, 02:55 PM   #7
Daemon Poster
 
Join Date: May 2009
Posts: 690
Default Re: php help

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????
biferi is offline   Reply With Quote
Old 11-02-2009, 03:16 PM   #8
Site Team
 
berry120's Avatar
 
Join Date: Jul 2009
Location: England, UK
Posts: 3,425
Default Re: php help

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.
berry120 is offline   Reply With Quote
Old 11-02-2009, 04:48 PM   #9
Daemon Poster
 
Join Date: May 2009
Posts: 690
Default Re: php help

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???
biferi is offline   Reply With Quote
Old 11-02-2009, 05:08 PM   #10
Site Team
 
berry120's Avatar
 
Join Date: Jul 2009
Location: England, UK
Posts: 3,425
Default Re: php help

Google analytics is the choice of many. AWStats is another open source one you might want to look at.

Quote:
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?
__________________

berry120 is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 05:54 AM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0