Loading...


bookmark - Make Your Own Very Simple Counter Using PHP!

Make Your Own Very Simple Counter Using PHP!

 
 Discussion by websaint with 36 Replies.
 Last Update: April 28, 2010, 6:58 pm ( View Rated (2) ) (View Latest)
Page 1 of 2 pages.
bookmark - Make Your Own Very Simple Counter Using PHP!  
Quickly Post to Make Your Own Very Simple Counter Using PHP!  w/o signup Share Info about Make Your Own Very Simple Counter Using PHP!  using Facebook, Twitter etc. email your friend about Make Your Own Very Simple Counter Using PHP! Print
Reply / Comment New Discussion / Topic Share / Bookmark E-Mail a Friend Print


Hi!! I'm going to show you how to make your own counter useing php!! :) It's really easy and you woun't have to keep seaching the net for free and bannerless counters.
So here comes the script:

CODE

<?php

//Simple class for counting visits
//Written by WebSaint

class Teller {
   function count() {
   $countfile = file("alle.txt");
   $count = $countfile[0];
   $count= $count + 1;
   $fp = fopen("alle.txt","w");
   $fw = fwrite($fp,$count);
   fclose($fp);
   echo $count;
   }
}

$obj =& new Teller;
$obj->count();

?>

Then add this to display the number of visitors:

CODE

<p>Number of guests visiting my site: <? include('alle.txt'); ?>.</p>


That's it. I hope you'll find the script useful! :)

   Tue Sep 28, 2004    Reply         

Good... but you might find others steal your counter if they find it...
they will link to your counter script, and store their page cound on your server.

its always good to add some kind of proetection againsed this. a good, but not perfect way is to make sure the referer comes from your own site, and nobody elses.

   Wed Sep 29, 2004    Reply         

QUOTE (qwijibow)

Good... but you might find others steal your counter if they find it...
they will link to your counter script, and store their page cound on your server.

its always good to add some kind of proetection againsed this. a good, but not perfect way is to make sure the referer comes from your own site, and nobody elses.
[post="3943"]<{POST_SNAPBACK}>[/post]


Well it's impossible to "steal" this. If you include that file from Websaints server the code will be treated as it was run on your server and thus will try to look file alle.txt from your server. If you simply request that file it will only add hits to Websaints counter.

The referer checking would be pointless in this but I'd like comment it a bit in general. People do not seem to know or just forget that nowadays quite many users disable or block referer sending. In certain browsers it is an option and I believe most of the current commercial software firewalls include an option to block them. Not taking this into account can mean that users that do not want to send referers can't use the service at all. For instance one development version of the WordPress blog software required referer sending to make changing it's options possible. The referer checking was simply just for sending the user back to the page where he came from. Other example is a certain torrent site that refuses to work if not getting referer information. They reason is understandable though: there is apparently countless "mirror" sites that are not actual mirrors but simply index their files and points the user to download the .torrents from the original site.

   Wed Sep 29, 2004    Reply         


QUOTE (Hercco)

Well it's impossible to "steal" this. If you include that file from Websaints server the code will be treated as it was run on your server and thus will try to look file alle.txt from your server. If you simply request that file it will only add hits to Websaints counter.

The referer checking would be pointless in this but I'd like comment it a bit in general. People do not seem to know or just forget that nowadays quite many users disable or block referer sending. In certain browsers it is an option and I believe most of the current commercial software firewalls include an option to block them. Not taking this into account can mean that users that do not want to send referers can't use the service at all. For instance one development version of the WordPress blog software required referer sending to make changing it's options possible. The referer checking was simply just for sending the user back to the page where he came from. Other example is a certain torrent site that refuses to work if not getting referer information. They reason is understandable though: there is apparently countless "mirror" sites that are not actual mirrors but simply index their files and points the user to download the .torrents from the original site.
[post="3949"]<{POST_SNAPBACK}>[/post]


you misunder stand my meaning.......

i mean someone could replace

CODE

<p>Number of guests visiting my site: <? include('alle.txt'); ?>.</p>


the include file with a coomplete URL to anouther server. and replace "alle.txt" with a different filename.

   Thu Sep 30, 2004    Reply         

But if you substitute the .txt file with a php file is't better?like this

CODE

<?php
<?php

$now=fopen("count.php","r+");
while(!feof($now)){
        $cont=fgets($now,2);
        if ($cont=="N") {
                              $check=fgets($now,3);
                                 if($check=="V=") {
                                 $nvisit=fgets($now,4);
                                 echo $nvisit;
                                 settype($nvisit,"integer");
                                 $posi=ftell($now);
                                 fseek($now,($posi-2));
                                 fwrite($now,($nvisit+1));
                                 exit;
                                }
}
}
fclose($now);


?>

and a count.php like this

<?php

NV=34

?>

   Thu Sep 30, 2004    Reply         

there are many scripting languages and you can find counter in each. first check which scripting is available on your server and then try to find its script for counter. i reccomond altavista.com rather than google.com for finding free scripts.

   Mon Oct 4, 2004    Reply         


QUOTE (qwijibow)

CODE

<p>Number of guests visiting my site: <? include('alle.txt'); ?>.</p>


the include file with a coomplete URL to anouther server. and replace "alle.txt" with a different filename.
[post="4056"]<{POST_SNAPBACK}>[/post]


And that would accomplish what?


QUOTE (sha)

there are many scripting languages and you can find counter in each. first check which scripting is available on your server and then try to find its script for counter. i reccomond altavista.com rather than google.com for finding free scripts.
[post="4380"]<{POST_SNAPBACK}>[/post]


We're DYI guys, we want to write our own scripts :)

   Mon Oct 4, 2004    Reply         

Why search or write you own counter ? if you want an easy way and a ads-less counter, just go to your astahost control panel, under Preinstalled Scripts / CGI Center / Counter, you can choose between 35 diferent types of counters and configure almost anything. After you choose your desired counter, press preview to see it how it's going to be and if you like, press make html and it will write a code to insert in your page.

Greets Deivid

   Mon Oct 4, 2004    Reply         

Why don't you use a beautiful flash counter connected with a php code. An example can be downloaded from flashkit.com tutorials.

   Mon Oct 4, 2004    Reply         

I know a very good free script named EliteStats.
You just have to put its folder in your main directory and include a one line of code in your webpage.
It will display How many visitors are online and complete statistics with IP address.

Sohail Ahmed
sohail4@msn.com

   Wed Oct 6, 2004    Reply         

I personally use and would highly recommend Statcounter, a free counter tracking service. It's so much more powerful than other free counters with its advanced visitor reporting and more.

   Fri Oct 8, 2004    Reply         

As I said in the other Forum I have also used statcounter and suggest you at least try it. It is ad free and u have the option to make it invisible or give it a range of colours. You can also with the one acount use it for multiple sitesand pages.

   Tue Oct 12, 2004    Reply         

ok I use the Mysql database to store the counter results. coz i have a database maintained already for the site...
the code is simple just fetch the row with the counter...
and use echo display on the page...
why bother about the security... The data base is secured...

   Wed Nov 17, 2004    Reply         

@Darren : seems to be interesting from what you say. I will give statcounter a try, and will compare to astahost's one. The one from astahost is also really impressive, I like it. Besides that I forgot my statcounter's password, and I am still waiting for the mail with it.
Yordan

OK, Darren, your are right, statcounter is really great. Their counters are not so nice as astahost ones, and the menus to step inside are not really easy to understand, and I experienced several disconnections in the middle of the process of creationg your counter. But if you want something really simple, without any ads, it's a simple line to be included inside your html source. And it works anywhere, including on your own PC for testing purposes, which is not the case for the astahost counter.
So, if you want something easy to implement, very nice, with a comfortable menu-diriven settings, use astahost's cpanel one. If you want something simple, which works on any site including your PC, use statcounter.

   Wed Sep 7, 2005    Reply         

Not too bad, it's a pretty simple counter, but it get's the job done.
I've been trying to figure out this one though, how do we make a download counter, that one is a bit harder isn't it? I suppose we'd make a page that would open the file's URL in a redirect, but also add to the counter and direct them to that instead of the counter?

   Wed Sep 7, 2005    Reply         

I like your script, it is clean and simple. It's a great use for light purposes. I use this to improve my website.

Although I like to use more heavy stats that give you greater detail in visitors.

   Mon Nov 14, 2005    Reply         

If you want one more secure counter, use MySQL Database!
You just have to:

CODE

CREATE TABLE website_counter
(visits int(10))

And put this code in the top of each page that you want to be counted:

CODE

$conn = mysql_connect("YOUR HOST","YOUR USER","YOUR PASSWORD") or die('MySQL said:  ' . mysql_error());
$db = mysql_select_db($db);
$sql = mysql_query("SELECT * FROM website_counter") or die('MySQL said:  ' . mysql_error());

while($visits = mysql_fetch_array($sql))
{
$visits = $dados["visits"];
$visits_new = $visits + 1;
}


$query = "UPDATE TABLE website_counter SET visits = $visits_new";
mysql_query($query, $conn) or die(mysql_error());


And where you want to show the number os visits:

CODE

echo ("Visits: $visits_new");


OBS.: I didn't test it. If you got any errors, post here.

   Thu Jan 26, 2006    Reply         

Hmm, I think the counter (at the beginning of this thread) can't be deloyed on busy site. Because if many pages read and write that file at once, there can be a error when accessing alle.txt

   Thu Jan 26, 2006    Reply         

@Alexandre : your program has a great advantage, you are independent from any provider, so you can use it on a server other than astahost, or even on a server on a private network.

QUOTE

OBS.: I didn't test it.

yes, it has to be tested first. For instance, I am curious to see where $dados is defined.

   Fri Jan 27, 2006    Reply         

For a simple php counter there's no need to use databases, flatfiles will do fine, the example by websaint is suffice as long as

CODE


<p>Number of guests visiting my site: <? include('alle.txt'); ?>.</p>


Is stored on the server and is not changeable or dependable on userinput.

Anyway, I was thinking that using an OOP approach here might be an overkill. What if you take the following PHP file

vc.php

CODE


<?php

  $countfile = file("alle.txt");
  $count = $countfile[0];
  $count= $count + 1;
  $fp = fopen("alle.txt","w");
  $fw = fwrite($fp,$count);
  fclose($fp);
  echo $count;
}
?>


Basically you know that whenever vc.php is called there is a new page visit so which means that you need the variable $count back.

So all you need to do now in your other php files is to have them include("vc.php") in the code.

eg

index.php

CODE


<?php
echo("hi");
include("vc.php");
?>


This would be failsafe for any crosssitescripting attempts since there's no userinput expected. Also, even if the name of vc.php is known so people can call it directly, it doesn't matter since they could only +1 to your counter, which is also possible if they reload your page 20+ times. Also, people still need to know the name of the counter script, which is quite difficult to guess if they don't see it stated somewhere.

Alternatively, you could also add an ip banning system to this script to check if a user has accessed this site before. Which is not really necessary if you want to know how much your page has been loaded.

About the busy website thing, it'd take quite a lot of requests per second to be faster than the opening and writing of one byte in a single file for it to have a deadlock. Also, If one has a busy site that can accomplish such thing, it'd be wiser to use a database approach since you can generate more statistics than just the amount of page visits (which is only interesting with a lot of visitors).

Ps. Anything flash is bad :)

   Fri Jan 27, 2006    Reply         

QUOTE (yordan)

@Alexandre : your program has a great advantage, you are independent from any provider, so you can use it on a server other than astahost, or even on a server on a private network.

yes, it has to be tested first. For instance, I am curious to see where $dados is defined.


Sorry, is not $dados['visits'], but $visits['visits']
Sorry again for the mistake.

   Fri Jan 27, 2006    Reply         

Actually... am a complete newbie to PHP.. dont know even a bit about it.. and all that scripting stuff... so was a lot confused in here... Will be learning it soon.. can anyone point out the link to the forum that offers PHP for newbies?

   Sun Mar 19, 2006    Reply         

QUOTE (sohahm)


I know a very good free script named EliteStats.
You just have to put its folder in your main directory and include a one line of code in your webpage.
It will display How many visitors are online and complete statistics with IP address.

Sohail Ahmed
sohail4@msn.com

Link: view Post: 4590


These scripts are so good, personally i was using them before begining to learn PHP, since that time i don't like to put any ready scripts on my site, i prefer to create it myself, that's the joy of PHP and other scripting languages, so i think better to post these useful links to these ready-made scripts on a new topic and to leave us enjoying this scripting battle :o
Personally i prefer the second counter, i like the combination between PHP/MySQL, more secured and better than ordinary text file, but ofcourse the first one is more simpler one.

   Sun Mar 19, 2006    Reply         

QUOTE (Alexandre Cisneiros)


OBS.: I didn't test it. If you got any errors, post here.

Link: view Post: 67865


it gives me this error:

CODE

You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'TABLE counter SET visits =' at line 1

i tried a lot but never got a result :o

   Tue Mar 21, 2006    Reply         

great i was going to make but now i see a premade one for me thanks people

   Mon Mar 27, 2006    Reply         

did anyone tried with this?, i tried alot but it keeps telling me there is an error :

QUOTE

You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'TABLE counter SET visits =' at line 1


hope anyone can tell us why it's not working.

   Sat Apr 8, 2006    Reply         

QUOTE (XIII)


did anyone tried with this?, i tried alot but it keeps telling me there is an error :
hope anyone can tell us why it's not working.

Link: view Post: 74858


Can you paste the line/part of the script that's causing this error ? It'll be easier to diagnose that way.

   Sat Apr 8, 2006    Reply         

QUOTE (miCRoSCoPiC)


Can you paste the line/part of the script that's causing this error ? It'll be easier to diagnose that way.

Link: view Post: 74863


That's the code for the whole script, ofcourse i'm putting the values for connect in my script, the counter table called "counter"

CODE

<?php
$conn = mysql_connect("localhost","db_username","db_pssword") or die('MySQL said: ' . mysql_error());
$db = mysql_select_db("db_name");
$sql = mysql_query("SELECT * FROM counter") or die('MySQL said: ' . mysql_error());

while($visits = mysql_fetch_array($sql))
{
$visits = $visits["visits"];
$visits_new = $visits + 1;
}


$query = ("UPDATE TABLE counter SET visits = '$visits_new' ");
mysql_query($query, $conn) or die(mysql_error());

echo ("visits : $visits_new");
?>

   Sat Apr 8, 2006    Reply         

QUOTE (XIII)




while($visits = mysql_fetch_array($sql))
{
$visits = $visits["visits"];
$visits_new = $visits + 1;
}
$query = ("UPDATE TABLE counter SET visits = '$visits_new' ");
mysql_query($query, $conn) or die(mysql_error());


Link: view Post: 74866


One error I can spot rightaway is with the $query statement. Why is the $visits_new variable within single quotes ? The function of the script is to update the table with the new value of $visits_new, right? In that case, it has to be enclosed with DOUBLE QUOTES in order for php to parse it and replace it with it's value.

Another factor that's confusing is that why's the statement enclosed by paranthesis ? ( $query = (...)) - those () are totally unnecessary here, although they shouldn't ideally cause a problem...

Try modifying that line to:

CODE


$query = 'UPDATE TABLE counter SET visits = "$visits_new" ';


Try this and let me know if it still produces the same error.

   Sat Apr 8, 2006    Reply         

QUOTE (miCRoSCoPiC)



Try modifying that line to:

CODE


$query = 'UPDATE TABLE counter SET visits = "$visits_new" ';


Try this and let me know if it still produces the same error.

Link: view Post: 74868


Now i got the same error, really so strange case to me :lol:

QUOTE

You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'TABLE counter SET visits = "$visits_new"' at line 1

   Sat Apr 8, 2006    Reply         

Quickly Post to Make Your Own Very Simple Counter Using PHP!  w/o signup Share Info about Make Your Own Very Simple Counter Using PHP!  using Facebook, Twitter etc. email your friend about Make Your Own Very Simple Counter Using PHP! Print
Reply / Comment New Discussion / Topic Share / Bookmark E-Mail a Friend Print


Similar Topics:

page counter with php+mysql

Most counter were realized with files. Here I suggest another solution which use php and mysql. A file named welcome.php contains the following code: <?php function welcome(){ $query= "update webcount set count=count+1;"; mysql_query( ...more

   04-Oct-2004    Reply         

A Simple Checking amp Validation ...

Hi, there is sometimes that you need to password protect a directory in your site but you dont have access to a database or you dont need it because only a few users will access this directory, well the following script i develop will help in this situation. With only 2 files you can im ...more

   08-Jun-2006    Reply         

Php Counter

A good way to figure out the traffic in your site and what pages need improvement is to keep a record of the visits of every page. In this tutorial, i will show you how to make a simple counter system. Simple counter: Keeps track of views of each page. Sh ...more

   20-Oct-2008    Reply         

(4) How To Create A PHP Based Hit Counter With MySQL  How To Create A PHP Based Hit Counter With MySQL