Jump to content



Welcome to AstaHost - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!
Photo
- - - - -

Make Your Own Very Simple Counter Using PHP!


38 replies to this topic

#1 websaint

websaint

    Member [ Level 1 ]

  • [HOSTED]
  • 43 posts

Posted 28 September 2004 - 08:58 PM

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:

<?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:

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

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

Edited by miCRoSCoPiC^eaRthLinG, 27 January 2006 - 07:23 AM.


#2 qwijibow

qwijibow

    Way Out Of Control - You need a life :)

  • Members
  • 1,366 posts
  • Location:Nottingham England
  • Interests:Computer / nerd related things (who would have guessed)<br />also. the following cartoons...<br /><br />South Park<br />King of the hill<br />Family guy<br />Sponge Bob Square pants<br />Simpsons

Posted 29 September 2004 - 03:23 PM

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.

#3 Hercco

Hercco

    Super Member

  • Members
  • 595 posts

Posted 29 September 2004 - 05:26 PM

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_SNAPBACK}>


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.

#4 qwijibow

qwijibow

    Way Out Of Control - You need a life :)

  • Members
  • 1,366 posts
  • Location:Nottingham England
  • Interests:Computer / nerd related things (who would have guessed)<br />also. the following cartoons...<br /><br />South Park<br />King of the hill<br />Family guy<br />Sponge Bob Square pants<br />Simpsons

Posted 30 September 2004 - 02:56 PM

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_SNAPBACK}>


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

i mean someone could replace

<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.

#5 k22

k22

    Advanced Member

  • Members
  • 165 posts
  • Location:IS - IT - US - BE

Posted 30 September 2004 - 04:03 PM

But if you substitute the .txt file with a php file is't better?like this
<?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

?>


#6 sha

sha

    Newbie [ Level 2 ]

  • Members
  • 18 posts

Posted 04 October 2004 - 02:02 PM

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.

#7 Hercco

Hercco

    Super Member

  • Members
  • 595 posts

Posted 04 October 2004 - 02:15 PM

<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_SNAPBACK}>


And that would accomplish what?


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_SNAPBACK}>


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

#8 deivid

deivid

    Member - Active Contributor

  • Members
  • 96 posts

Posted 04 October 2004 - 05:17 PM

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

#9 antitoxic

antitoxic

    Member [ Level 1 ]

  • Members
  • 46 posts

Posted 04 October 2004 - 05:43 PM

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

#10 sohahm

sohahm

    Newbie [ Level 2 ]

  • Members
  • 13 posts

Posted 06 October 2004 - 05:13 PM

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

#11 jcguy

jcguy

    Premium Member

  • Members
  • 382 posts

Posted 08 October 2004 - 03:51 PM

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.

#12 Darren

Darren

    Premium Member

  • Members
  • 207 posts
  • Gender:Male
  • Location:Vic, Australia
  • Interests:Obviously computers and programing basics and computer games. Footy (Aussie rules [Best game in the world!!!]) and TV.

Posted 13 October 2004 - 05:57 AM

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.

#13 thedevil

thedevil

    Member - Active Contributor

  • Members
  • 82 posts

Posted 18 November 2004 - 04:52 AM

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...

#14 yordan

yordan

    Way Out Of Control - You need a life :)

  • [MODERATOR]
  • 4,677 posts

Posted 07 September 2005 - 08:49 AM

@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.

#15 little0run

little0run

    Advanced Member

  • Members
  • 114 posts

Posted 07 September 2005 - 09:19 PM

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?

#16 Jikson26

Jikson26

    Newbie [ Level 1 ]

  • Members
  • 2 posts

Posted 15 November 2005 - 06:25 AM

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.

#17 Alexandre Cisneiros

Alexandre Cisneiros

    Newbie [ Level 2 ]

  • Members
  • 24 posts

Posted 26 January 2006 - 10:58 PM

If you want one more secure counter, use MySQL Database!
You just have to:
CREATE TABLE website_counter
(visits int(10))
And put this code in the top of each page that you want to be counted:

$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:
echo ("Visits: $visits_new");

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

#18 PureHeart

PureHeart

    Premium Member

  • Members
  • 212 posts
  • Location:RMIT University
  • Interests:IT, English, PC, Internet, Traditions and Customs
  • myCENTs:65.61

Posted 27 January 2006 - 07:05 AM

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

#19 yordan

yordan

    Way Out Of Control - You need a life :)

  • [MODERATOR]
  • 4,677 posts

Posted 27 January 2006 - 02:00 PM

@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.

OBS.: I didn't test it.

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

#20 jipman

jipman

    Super Member

  • Members
  • 733 posts

Posted 27 January 2006 - 07:14 PM

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

<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
<?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
<?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 :)



Reply to this topic



  


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users