Easy Visitor Counter - Easy to use and to install

free web hosting
Free Web Hosting > Computers & Tech > Programming > Scripting > PHP

Easy Visitor Counter - Easy to use and to install

jsuthers
Add this to the page you want to count the visitors on:

CODE
<?php
$logfile="counter.txt";
$returnpage = htmlentities($_GET['count']);
if (! @$file = fopen($logfile,"r+"))
{
$count="1";
}
else
{
$count = @fread($file, filesize($logfile)) or $count=0;
fclose($file);
$count++;
}
$file = fopen($logfile,"w+");
fputs($file, $count);
fclose($file);
?>


The link to the page you want to count the visitors should be www.yoursite.com/yourpage.php?count
The visitors numbers will be saved in counter.txt

Add this to the page where you want to show how many visitors the page have had:
CODE
<?php
$logfile="counter.txt";
if (! @$file = fopen($logfile,"r+"))
{
$count="0";
}
else
{
$count = @fread($file, filesize($logfile)) or $count=0;
fclose($file);
}
echo 'Visitors: <font color="#FF6600">'.$count.'</font>.';
?>


 

 

 


Reply

sparkx
That seems like a lot of work to count the number of visitors. Also how safe is it? I get a little worried when you try to edit actual files rather then using a database. This is how I would count visitors using a database:
CODE
<?php
//Update
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());
$row = mysql_fetch_array(mysql_query("SELECT * FROM info WHERE ID='1'") or die(mysql_error()));  
$count = $row['count']+1;
$result = mysql_query("UPDATE info SET count='$count' WHERE ID='1'") or die(mysql_error());  

//Display
echo 'Visitors: <font color="#FF6600">'.$count.'</font>.';
?>

This method is great and very secure unless you don't have MySQL access. Otherwise your script would be the only option and would work good as long as you made sure your server only allowed editing of that file only and only from the server itself (not an external server/hacker).

Your script is still good to know though because when you get into more advanced things you might need to actually save a file to the server (for example when someone is installing your open source code on their server).

Sparkx

 

 

 


Reply

FirefoxRocks
Yes I think that a database would be a better idea as it requires a password to access and is less work to use than by writing to a file each time. This is because unless you have specific permissions set, anyone can read that file and that may not be a good thing.

With a database you can do so much more though, such as IP logging, browser (user-agent) identification, etc. This may or may not be useful but to most people it should be because it helps efficiently target their audience in some cases.

Reply

magiccode9
But sometime that only a value then it need to be read from a db may waste it space.
If the code was used on small sites would be fine.

Writing to file just as to database.
But instead I am thinking about the code changed a bit may be better by first initial the variables.

$count = 0;
$file = null;

if ( ... )
....

Reply


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Recent Queries:-
  1. visit counter - 33.29 hr back. (1)
  2. visiter counter for website - 44.21 hr back. (1)
  3. visiter counter in php - 45.43 hr back. (1)
  4. count visitor - 69.08 hr back. (1)
  5. simple unique visitors counter html - 85.52 hr back. (1)
  6. visiter count php script - 101.83 hr back. (1)
  7. php page visitor counter - 158.23 hr back. (1)
  8. visiter counter - 89.26 hr back. (4)
  9. simple visitor online counter script - 162.47 hr back. (2)
  10. visiter online couter - 166.89 hr back. (1)
  11. count of the visiter - 172.59 hr back. (1)
  12. visitorcounter php text file - 186.92 hr back. (2)
  13. visiter counter php - 187.74 hr back. (2)
  14. visiter counter with php - 214.32 hr back. (1)
Similar Topics

Keywords : easy, visitor, counter, easy, install

  1. Mccodes V2
    i just bought mccodes and i cant install it (6)
  2. How To Create An Online Timed Test With PHP?
    Can we do a online test with a counter(from 20 second) php for a php-n (17)
    Is there anyone got a online timed test script?Or anyone knows how to create this script? I wait
    your answers and all php programmers.....
  3. Link Counter
    (3)
    i have a game site and i wanted to know how can i create a link counter for every game i have and i
    want it all to use the same script not have to use like 10 diffrent scripts how can i make this or
    is there any site that offers the script for free? ....
  4. Counter With Img In Flat File
    (0)
    this is a counter with images and stor in flat file becouse i can not upload .zip .rar file iwell
    program it on this post at frist you need to 2 files count.php count.txt and you need else make
    folder has name gifs and make 10 pictuer 10 file 0.gif to 9.gif now all ok open the count.php and
    add this code CODE <? ### IMAGE FORMAT $format = ".gif"; $file =
    file("count.txt"); $num = ($file[0] + 1);
    exec("echo $num > count.txt"); switch($type) { case "....
  5. Php Problem - Unique Counter
    my script isn't working correctly (4)
    alright here's what I want to do: I made a counter to count unique visitors without a mysql. But
    the problem is that the '.dat' file I'm writing on (unique.dat) tends to become too big
    when I have too many visitors. Thus, I tried to add an if/then statement. Kay, now here's the
    problem: even though this code successfully runs, it's not a hundred-percent accurate. Sometimes
    it rewrites a user's IP even though it is already on the '.dat' file. Even though it
    doesn't actually affect the $hits, I'd still like to know what I ....
  6. How I Can Install PHP On My PC
    (14)
    hi every body i have installed IIS on my windows(XP), now i need help to install php? i read many
    helps for this but i can understand , please offer simple for me....
  7. Where Can I Get Counter?
    for my homepage (8)
    can anyone recommend good free counter service /rolleyes.gif' border='0'
    style='vertical-align:middle' alt='rolleyes.gif' /> thank u sir /smile.gif' border='0'
    style='vertical-align:middle' alt='smile.gif' /> ....

    1. Looking for easy, visitor, counter, easy, install

Searching Video's for easy, visitor, counter, easy, install
advertisement




Easy Visitor Counter - Easy to use and to install



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE