Creating a Counter in PHP (Difficulty: Beginner)

Josh-Josh- Royal Oak, MI
edited October 2004 in Internet & Media
Creating a Counter in PHP
-- written by Josh

You will need to create 2 files for this guide;
  • count.txt - used to store hits on your site.
  • counter.php - used to record hits on your site.

First, copy and paste this to your counter.php file:
[php]
<?php
if($count == ""){
setcookie("count", "m");
$file = file("count.txt");
$num = ($file[0] + 1);
exec("echo $num > count.txt");
}
?>
[/php]

Now, lets decifer which does what, starting with
[php]$file = file("count.txt");[/php]
What this does it determines the file that every hit on your site will be recording to, this file can be changed or set to a variable if you wish to modify the code somewhat.


[php]
$num = ($file[0] + 1);
[/php]
After the $file variable is defined to count.txt, this function adds 1 to the number that is stored in your count.txt file. You could also amend it to subtract instead of add, ect.

Displaying the Number of Hits on your Page
[php]
exec("echo $num > count.txt");
[/php]
Displays the number recorded in your count.txt file on the page, it echoes (prints) the number onto your page.

[php]
if($count == ""){
setcookie("count", "m");
[/php]
This tries to make sure that your hit is not counted twice or more, ect.

I'd say thats pretty much all to know.

Have fun with your new site counter,
-Josh
-.-

Comments

  • Josh-Josh- Royal Oak, MI
    edited November 2003
    A Different Version
    -- written by Josh

    [php]
    <?php
    $filename = count.txt

    if($count == ""){
    setcookie("count", "m");
    $file = file("$filename");
    $num = ($file[0] + 1);
    exec("echo $num > $filename");
    }
    ?>
    [/php]
    Count.txt only has to be modified at $filename..rather then modifying it at multiple errors. For people who wish to modify the file that hits are recorded to, if the code is made more complex. Other then that, the code is pretty simple, but there are a lot of beginners that I guess could get confused.
  • Josh-Josh- Royal Oak, MI
    edited November 2003
    Hits by Unique IP
    -- Written by 02 from www.codexx.tk

    [php]
    <?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);
    ?>
    [/php]
  • Josh-Josh- Royal Oak, MI
    edited December 2003
    Made a mistake, to show the number of hits on your page paste this where you want them to appear:
    [php]
    <? include("count.txt"); ?>
    [/php]
  • swooshswoosh Ptuj, Slovenia
    edited October 2004
    Hey!

    I am kind of new in PHP and I would like to add a counter on one simple page.

    I copied this into my counter.php file:

    <?php
    if($count == ""){
    setcookie("count", "m");
    $file = file("count.txt");
    $num = ($file[0] + 1);
    exec("echo $num > count.txt");
    }
    ?>

    Then I created the count.txt file in the same folder as counter.php.

    After trying to open view if counter.php works, I get the error: Parse error: syntax error, unexpected T_ECHO in C:\Webpage\counter.php on line 6.

    Any ideas what's wrong?

    Thanks for all your help!
  • ShortyShorty Manchester, UK Icrontian
    edited October 2004
    [php]
    <?php
    if($_COOKIE == "")
    {
    setcookie("count", "m");
    $file = file("count.txt");
    $num = ($file[0] + 1);
    echo $num;
    }
    ?>
    [/php]

    :)
  • swooshswoosh Ptuj, Slovenia
    edited October 2004
    Shorty wrote:
    [php]
    <?php
    if($_COOKIE == "")
    {
    setcookie("count", "m");
    $file = file("count.txt");
    $num = ($file[0] + 1);
    echo $num;
    }
    ?>
    [/php]

    :)

    LOL ... OK .. new error (they just keep on coming, don't they? :bawling: )

    Parse error: syntax error, unexpected $end in C:\Webpage\counter.php on line 9.

    Argghhh ...

    :scratch:
  • ShortyShorty Manchester, UK Icrontian
    edited October 2004
    Ok.. let me take a look at this.. that'l teach me not to check it first ;D

    /me reaches for his coffee and has a look more closely..
  • ShortyShorty Manchester, UK Icrontian
    edited October 2004
    No. works fine here...

    Paste your code as you have it please :)

    Btw... errors are normal when you first start learning ;)
  • swooshswoosh Ptuj, Slovenia
    edited October 2004
    Shorty wrote:
    No. works fine here...

    Paste your code as you have it please :)

    My bad ... ;)

    It's working now. I overlooked something. Thanks for your help. :thumbsup:
  • ShortyShorty Manchester, UK Icrontian
    edited October 2004
    Missed a ; or a } somewhere then ;D
  • swooshswoosh Ptuj, Slovenia
    edited October 2004
    Shorty wrote:
    Missed a ; or a } somewhere then ;D

    Not really .. actually it was a " missing. :scratch:

    Btw ... do you use anykind of "chat" programms ... like MSN or something .. you know ... if I would like some more help. ;)
  • ShortyShorty Manchester, UK Icrontian
    edited October 2004
    Actually.. paste a thread instead of IM (im not a big lover of instant messenger apps) :)

    I am always logged into this site (except when I sleep and that ain't often) and will answer ya questions.

    Threads about PHP promote interest from various people around here, so you can garner the responses of more than just one :)
  • edited October 2004
    Hey, if u don't want to learn and just want a PHP counter then there are many availble on the internet for free, if u want stats ability i would sugest Free (also adfree) http://www.statcounter.com :thumbsup:
  • swooshswoosh Ptuj, Slovenia
    edited October 2004
    AbuKing wrote:
    Hey, if u don't want to learn and just want a PHP counter then there are many availble on the internet for free, if u want stats ability i would sugest Free (also adfree) http://www.statcounter.com :thumbsup:

    I am planning to learn PHP but the counter was just something that I needed now.

    If anybody has a great idea on how to learn programming in PHP .. I would really appreciate it. Are there "easy" ways of learning PHP? Is there a book or something like that. I know there is a manual on www.php.net ...

    Well .. thanks for now.

    You are all great! :thumbsup:
Sign In or Register to comment.