Jump to content



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

Php Unique Hit Counter


54 replies to this topic

#1 maddog39

    Super Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 208 posts
  • Location:LI, New York
  • Interests:Web programming, computer programming, computer games, and Play Station2. :D

Posted 07 March 2005 - 11:12 PM

Hello all,

Here is a neat and helpful PHP script that can count unique page views on your website. First you need to open up a new page in your text editor and paste in this code.
<?php
$filename = "hits.txt";

$file = file($filename);
$file = array_unique($file);
$hits = count($file);
echo $hits;

$fd = fopen ($filename , "r");
$fstring = fread ($fd , filesize ($filename));
fclose($fd);
$fd = fopen ($filename , "w");
$fcounted = $fstring."\n".getenv("REMOTE_ADDR");
$fout= fwrite ($fd , $fcounted );
fclose($fd);
?>
then save the page as counter.php

Next open up another new page in your text editor and just save it as hits.txt
Then upload all of the files onto your server and CHMOD hits.txt to 0777 or just 777.
After that open up your main page or the page you will be placing the counter on and pste in the following code.
<?php
include ('counter.php');
?>
then save and close that page. If needed, re-upload the file(s) to your server. Very simple and easy to use. Comments are allways appreciated. :D :D

#2 millertime

    It's Miller Time

  • Kontributors
  • PipPipPipPipPipPipPipPip
  • 168 posts

Posted 08 March 2005 - 02:44 AM

wow, thats a really short and simple script... thanks

#3 NotoriousZach

    Member [Level 1]

  • Kontributors
  • PipPipPipPip
  • 55 posts

Posted 08 March 2005 - 03:17 AM

Yeh is really nice, im gonna test it out, i alrady have one that just counts page hits, one that counts unique page hits, EVEN BETTER!

#4 rejected

    Super Nuclear Advanced Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 743 posts
  • Gender:Male
  • Location:Texas
  • Interests:I enjoy running cross country and track, and the occasional cocktail with some friends.
  • myCENT:51.02

Posted 08 March 2005 - 03:30 AM

Very nice! All of the other ones I've seen are very complex and I have no idea what they mean, but I understand this one. Do you mind if I use it on my site?

#5 karlo

    Privileged Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 632 posts
  • Gender:Male
  • Location:Philippines
  • myCENT:54.44

Posted 08 March 2005 - 04:48 AM

Do you think that we must use Cookies to make a UNIQUE COUNTER SCRIPT? What do you think?

#6 rejected

    Super Nuclear Advanced Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 743 posts
  • Gender:Male
  • Location:Texas
  • Interests:I enjoy running cross country and track, and the occasional cocktail with some friends.
  • myCENT:51.02

Posted 08 March 2005 - 01:10 PM

I just added it to my site, but it's not unique hits. :D

#7 maddog39

    Super Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 208 posts
  • Location:LI, New York
  • Interests:Web programming, computer programming, computer games, and Play Station2. :D

Posted 09 March 2005 - 10:55 PM

It counts unique. The first couple hits are counted as page views till it kicks in. try it on my site at the bottum of the page. http://gamerspub.trap17.com :angry:

#8 karlo

    Privileged Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 632 posts
  • Gender:Male
  • Location:Philippines
  • myCENT:54.44

Posted 10 March 2005 - 04:51 AM

maddog39, on Mar 10 2005, 06:55 AM, said:

It counts unique. The first couple hits are counted as page views till it kicks in. try it on my site at the bottum of the page. http://gamerspub.trap17.com :angry:

View Post

Well, here's what happened.

I visited your website. After that, I saw the counter. Then I reloaded the page. It stays the same. Then I reloaded it again, then the counter increased by +1. After that, I reloaded and reloaded the page again. It stays the same. Then I went to http://gamerspub.trap17.com/hits.txt and I saw many logs of my ip address.

You must use cookies() in order for it to work well. Or sessions.

#9 maddog39

    Super Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 208 posts
  • Location:LI, New York
  • Interests:Web programming, computer programming, computer games, and Play Station2. :D

Posted 10 March 2005 - 09:21 PM

In my book thats not needed and it makes things complicated. It can mis count thats true. However its sensitive in different ways. So what you said depends. :angry:

#10 karlo

    Privileged Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 632 posts
  • Gender:Male
  • Location:Philippines
  • myCENT:54.44

Posted 11 March 2005 - 09:58 AM

maddog39, on Mar 11 2005, 05:21 AM, said:

In my book thats not needed and it makes things complicated. It can mis count thats true. However its sensitive in different ways. So what you said depends. :angry:

View Post

Ok, maybe i'll try to modify your code and improve it. Don't worry, i'll put your copyright. Can you please add some comments and explaination to your code and add it again here, in your thread?




Reply to this topic


This post will need approval from a moderator before this post is shown.

  


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users