welcome

welcome to my destiny :)
I'm just a guy who is smile a lot
and the god give me a power
-------------------------------------------

You say anything Just tell me all your sweet lies

-------------------------------------------------

Time may change my life
But my heart remains the same to you
Time may change your heart
My love for you never changes

Saturday, June 7, 2008

How to make a Hit Counter with PHP

PHP Hit Counter

In this lesson we are going to make a very simple hitcounter.

A hitcounter is a script, that counts the number of visitors to your site.

You actually have 2 kinds of hitcounter:

1.

visable hitcounters ( every visitor can see the number of “hits” )
2.

“invisable” hitcounters ( only visable to you, the webmaster)

In this lesson we are going to make a simple visible hitcounter .
Who can learn PHP (and follow this tutorial) ?

Well, to be short everyone can.

But if you don't know anything about HTML, it is going to be very difficult.

HTML is a must for every webdesigner/webdeveloper , so you should learn HTML first, if you don't know it already.

I expect you to be quite familiar with Internet and WWW

What should the hitcounter do?

Every time a surfer on the Internet visits your page the number of hits should increase by one.

Sounds simple, not?

Well, actually, it is simple too.
Start coding

First, choose the page , that you want to count the hits for . The best choice is to take your intropage of your homepage, probably called index.html of index.htm
Just rename this file to index.php. Now we make another file called counter.txt ,
just leave this file “counter.txt” completely empty.

Now for the real script:

open the file “index.php” in a simple text editor like Notepad

Place all the PHP code we are going to write now on the place in your HTML code , where you want your counter to appear.

first we will start with the opening and closing tags of PHP.

The opening tag for PHP looks like this:



Everything between those tags is considered actual PHP code by the PHP interpreter on the webserver.

Now, we are going to make our first variable.

Type this:

$filename= "counter.txt" ;

This means that now there is a variable called “filename” that has the value “counter.txt”.

Don't forget the semi-colon ( ; ) , you should end every line of code with this character, or else the PHP interpreter will give you an error.

Now we are going to write the code, that is going to show the number of hits until now:

Type this:

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

This is what this code does:

the function “fopen” opens the file “counter.txt” in reading mode ( “r”).

the function “fread” reads the contents of the file we just opened and assigns this content to the variable called ”$fstring”.

The next line:

echo “$fstring” means that the contents of this variable is shown on your monitor.

Now we are going to write the “counting” part of the script.

$fd = fopen ($filename , "w") or die ("Can't open $filename") ;
$fcounted = $fstring + 1 ;
$fout= fwrite ($fd , $fcounted ) ;
fclose($fd) ;

Here we again open the file called “counter.txt” , but now in writing mode.

We 'll use the variable $fstring again and increment it with one.

Then this value is written to the counter.txt file (fwrite) and the file is closed (fclose).

This is the complete script:

$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) ;

?>

If you made no typo's , save this file as “index.php”.


If you now upload this file and the counter.txt file to a webserver that supports PHP, you can see the script working.

When you push the “reload” or “refresh” button of your browser, you'll notice that the number is increasing.

If it doesn't work, this could have several causes:

-you made an typing error

-check if your webserver supports PHP

-it could be that you have to chmod the script and the counter.txt file ( on Unix/Linux servers).

The script itself should be worldexecutable ( 755) and the counter.txt file should be worldwritable (777).

I hope you learned something here and maybe it is of some use to you.

No comments:

LinkWithin

Related Posts Plugin for WordPress, Blogger...

Fm-radio-on-my-site

for you forever

Birthday - 13/12/1964 Death - 02/05/1997 bye bye hide

google analysis

ຊີວິດກໍ່ເໝືອນລະຄອນຕ່າງທີ່ບໍ່ສາດມາດຫຼິ້ນຄືນໃໝ່ໄດ້
ຊີວິດແມ່ນການສະແດງ ທີ່ບໍ່ມີຜູ້ກຳກັບ

online
Online Casino


website counter