Count the visitor of website

This is a discussion on Count the visitor of website within the PHP Language forums, part of the PHP Programming Forums category; Dear All, i have get combination of four PHP script for count the visitor of website but when i apply ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 09-29-2007
Dev
 
Posts: n/a
Default Count the visitor of website

Dear All,
i have get combination of four PHP script for count the visitor of
website but when i apply all these i have no answer.
i am posting the code of all four PHP script below.

CODE OF INDEX.PHP
<?php

print "<font face='verdana' size='1'>";

If (file_exists( "counter.txt" )){
include_once "read.php";

$count++;

echo "This page has been visited <b>$count</b> times";

include_once "write.php";
} else {
print "<font color='red'><b>Error</b>: Cannot find Counter Data File!
</font>";
}

print "</font>";

?>


CODE OF COUNT.PHP
<?php

print "<font face='verdana' size='1'>";

If (file_exists( "counter.txt" )){
include_once "read.php";

$count++;

include_once "write.php";
} else {
print "<font color='red'><b>Error</b>: Cannot find Counter Data File!
</font>";
}

print "</font>";

?>

CODE OF READ.PHP
<?php
$file = "counter.txt";

$open = fopen( $file, "r" );
$count = fread ( $open, filesize ( $file ) );
fclose( $open );
?>

CODE OF WRITE.PHP
<?php
$file = "counter.txt";

$open = fopen( $file, "w" );
fwrite( $open, $count );
fclose( $open );
?>

THERE IS ONE FILE ALSO NAMED COUNTER.TXT HAVING 0

any body help me to apply these correctly because i am new to PHP.
Please tell me
1. correct location of each script in my index.html page
2. any extra code (if any) for the index.html page.

thanks in advance.

Dev

Reply With Quote
  #2 (permalink)  
Old 09-29-2007
Jerry Stuckle
 
Posts: n/a
Default Re: Count the visitor of website

Dev wrote:
> Dear All,
> i have get combination of four PHP script for count the visitor of
> website but when i apply all these i have no answer.
> i am posting the code of all four PHP script below.
>
> CODE OF INDEX.PHP
> <?php
>
> print "<font face='verdana' size='1'>";
>
> If (file_exists( "counter.txt" )){
> include_once "read.php";
>
> $count++;
>
> echo "This page has been visited <b>$count</b> times";
>
> include_once "write.php";
> } else {
> print "<font color='red'><b>Error</b>: Cannot find Counter Data File!
> </font>";
> }
>
> print "</font>";
>
> ?>
>
>
> CODE OF COUNT.PHP
> <?php
>
> print "<font face='verdana' size='1'>";
>
> If (file_exists( "counter.txt" )){
> include_once "read.php";
>
> $count++;
>
> include_once "write.php";
> } else {
> print "<font color='red'><b>Error</b>: Cannot find Counter Data File!
> </font>";
> }
>
> print "</font>";
>
> ?>
>
> CODE OF READ.PHP
> <?php
> $file = "counter.txt";
>
> $open = fopen( $file, "r" );
> $count = fread ( $open, filesize ( $file ) );
> fclose( $open );
> ?>
>
> CODE OF WRITE.PHP
> <?php
> $file = "counter.txt";
>
> $open = fopen( $file, "w" );
> fwrite( $open, $count );
> fclose( $open );
> ?>
>
> THERE IS ONE FILE ALSO NAMED COUNTER.TXT HAVING 0
>
> any body help me to apply these correctly because i am new to PHP.
> Please tell me
> 1. correct location of each script in my index.html page
> 2. any extra code (if any) for the index.html page.
>
> thanks in advance.
>
> Dev
>


First of all, enable all errors and display them (see your php.ini
file). You will see your error.

You need to have something in your file before you can read it. Just
initialize it to 0 (numeric zero) with your favorite editor and it
should be ok.

Also, ensure the webserver userid has read/write access to the file.

And you have another potential problem which will show up sooner or
later - two different people accessing the file at the same time. You
may just lose one update - or you could screw up the file altogether.
You really need to lock the file from before you open it for reading
until after you close it for writing.

As for where to put it? Something like this can go anywhere between the
<body> and </body> tags - depends on where you want to display it.

But also, just forget it. A counter on a page is a sure sign of a
newbie (or someone who needs their ego stroked :-) ). Rather. use the
logs to get the information.


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Reply With Quote
  #3 (permalink)  
Old 09-29-2007
Sanders Kaufman
 
Posts: n/a
Default Re: Count the visitor of website


"Dev" <hanudev@gmail.com> wrote in message
news:1191033370.411232.211310@50g2000hsm.googlegro ups.com...
> Dear All,
> i have get combination of four PHP script for count the visitor of
> website but when i apply all these i have no answer.
> i am posting the code of all four PHP script below.


I've got a script that does that pretty well:

[code]
class hitcounter{
var $HitCount;
var $DataFile;
function hitcounter(){
$this->DataFile = "hitcount.txt";
$this->HitCount = $this->GetHitCount();
}
function GetHitCount(){
$sTMP = file_get_contents($this->DataFile,false);
if($sTMP < 1){
$iRetVal = 1;
} else {
$iRetVal = $sTMP;
}
$this->HitCount = $iRetVal;
return $iRetVal;
}
function AddHit(){
$iRetVal = (++$this->HitCount);
$oFile = @fopen($this->DataFile,"w",false);
$iBytes = fwrite($oFile,$iRetVal);
$bVoid = fclose($oFile);
$this->HitCount = $iRetVal;
return $iRetVal;
}
}
$oCounter = new bvckvs_hitcounter();
echo ("<br>Page Visits:" . $oCounter->AddHit() );

[code]


Reply With Quote
  #4 (permalink)  
Old 09-29-2007
klenwell
 
Posts: n/a
Default Re: Count the visitor of website

On Sep 28, 7:36 pm, Dev <hanu...@gmail.com> wrote:

>
> any body help me to apply these correctly because i am new to PHP.
> Please tell me
> 1. correct location of each script in my index.html page
> 2. any extra code (if any) for the index.html page.
>
> thanks in advance.
>
> Dev



If you're interested in a more sophisticated php-based hit counter, I
suggest checking out pphlogger:

http://pphlogger.phpee.com/download.php

I used it for a while before Google Analytics. I wouldn't recommend
installing it for a production site as it hasn't been actively
supported for a few years and it uses deprecated functionality like
register globals.

But for some insights into how to collect data (like Google Analytics,
it is designed to allow you to track metrics on a remote site using
javascript and img tags) and what sort of information you can collect,
it's worth poking around the code. It's not quite as elegant as
Sanders's script. But it can be a good learning experience, if
nothing else.

Tom

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 10:28 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0