Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> Easy Visitor Counter, Easy to use and to install
jsuthers
post May 24 2008, 08:58 PM
Post #1


Newbie [ Level 1 ]
Group Icon

Group: Members
Posts: 1
Joined: 24-May 08
Member No.: 30,548



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>.';
?>


Go to the top of the page
 
+Quote Post
sparkx
post May 25 2008, 03:43 PM
Post #2


Sparkx
Group Icon

Group: [HOSTED]
Posts: 354
Joined: 11-October 06
From: Dana Point, CA, USA
Member No.: 16,496



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
Go to the top of the page
 
+Quote Post
FirefoxRocks
post May 25 2008, 03:55 PM
Post #3


Super Member
Group Icon

Group: [HOSTED]
Posts: 696
Joined: 12-July 06
From: Ontario, Canada
Member No.: 14,464



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.
Go to the top of the page
 
+Quote Post
magiccode9
post Jun 1 2008, 04:52 PM
Post #4


Member - Active Contributor
Group Icon

Group: [HOSTED]
Posts: 84
Joined: 7-November 05
Member No.: 9,489



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 ( ... )
....
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. How I Can Install PHP On My PC(14)
  2. Counter With Img In Flat File(2)
  3. Link Counter(3)
  4. How To Create An Online Timed Test With PHP?(18)
  5. Mccodes V2(6)


 



- Lo-Fi Version Time is now: 12th October 2008 - 03:00 PM