473,387 Members | 1,882 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Count the visitor of website

Dev
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</btimes";

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

Sep 29 '07 #1
3 6344
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</btimes";

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
<bodyand </bodytags - 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.
js*******@attglobal.net
==================
Sep 29 '07 #2

"Dev" <ha*****@gmail.comwrote in message
news:11**********************@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]
Sep 29 '07 #3
On Sep 28, 7:36 pm, Dev <hanu...@gmail.comwrote:
>
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

Sep 29 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Bob Bedford | last post by:
Hello there, I'd like to get the visitor's email (not for spamming....) It's to avoid to show some content to my competitors....
2
by: Mounir | last post by:
a website opens in my iframe Is there a way to know the address of the link the visitor clicks on ?
2
by: Hari | last post by:
Hi, How can I find out what site a visitor has come from using ASP? Suppose my website is sample.com and I have one page in the website that is used by the visitor to request products and...
12
by: FluffyCat | last post by:
New on November 28, 2005 for www.FluffyCat.com PHP 5 Design Pattern Examples - the Visitor Pattern. In the Visitor pattern, one class calls a function in another class and passes an instance of...
2
by: radiox | last post by:
Hi, I build a website who can monitor other website's visitors and build some reports. I use PHP/MySQL and Javascript. I am stucked at the point when I have to get the page where the...
18
by: damezumari | last post by:
I would like to know how many of the visitors to my site has js enabled and how many has it turned off. I haven't found a simple solution searching Google groups so I suggest the following using...
3
by: bush | last post by:
hi everyone! i want to get the ipaddress,and the country name of a webpage visitor,using asp.Net(C#).if anyone knows about it,plz rply me. thanx in advance.
32
by: Ciaran | last post by:
Hi I've seen this a few places - The site lists off the number of people (not logged in) currently browsing the site. How can I do this with php / mySQL please?
0
by: Dev | last post by:
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.