SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Member
    Join Date
    Feb 2003
    Location
    virginia
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    very simple counter

    Does anyone know code to make a very simple counter, basically all it does it record the total hits on the page it is put on? I don't want to use some pre-made script by somebody else, since you can never get exactly what you want and it usually has a lot of extra stuff.

    Just a simple counter script. Preferably something in which I can change the bgcolor and text color (put it in a table?)

  2. #2
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    SvÝ■jˇ­
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not in javascript, which is executed on the client, i.e. can't save info about other visitors hitting the same page...

  3. #3
    SitePoint Member
    Join Date
    Feb 2003
    Location
    virginia
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But i've seen JS counters before.

  4. #4
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you want the counter to be a total site-hit counter, a unique site-hit counter, or a unique/total per-person counter?

    Regards.
    Someonewhois

  5. #5
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    SvÝ■jˇ­
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm, you can use JS to trigger the counter, but the actual counting is done on the server

  6. #6
    SitePoint Addict
    Join Date
    Aug 2001
    Location
    Los Angeles, CA
    Posts
    346
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can't create a counter with JavaScript, but you can create a very simple one with PHP:
    Code:
    <?
    #####* CHANGES REQUIRED *#####
    $cf = "cnt.dat" ; #* The counterfile.
    $path = "./pix/" ; #* Path to images.
    $ft = "gif" ; #* gif, jpg or png.
    $ti = "text" ; #* text or images.
    #####* NO CHANGES HERE *#####
    $fp = @fopen($cf, "r" ) ;
    $cn = @fread($fp, 255) ;
    @fclose ($fp) ;
    $fp = fopen ($cf, "w" ) ;
    $cn++ ;
    fputs ($fp, "$cn" ) ;
    fclose ($fp) ;
    if ($ti == "images" ) {
    for ($i = 0; $i <= 9; $i++) {
    $cn = str_replace($i, "<img src=""\"$path$i.$ft\">", $cn) ;
    }
    }
    echo "$cn<br>" ;
    ?>
    Save the above code in a file called "counter.php", and then implement the counter into your page with this:

    <?php include("counter.php"); ?>
    Last edited by gregc; May 29, 2003 at 11:20.

  7. #7
    SitePoint Member
    Join Date
    Feb 2003
    Location
    virginia
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, doing now.

  8. #8
    SitePoint Member
    Join Date
    Feb 2003
    Location
    virginia
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Uhh, that php counter isn't working. Or more likly I am not doing it right.

    I would like a total hit counter (records hit on just one page) - no cookies or IP logging or anything.

    I have my own webspace, if that is any help (purchased, not free).

  9. #9
    SitePoint Addict
    Join Date
    Aug 2001
    Location
    Los Angeles, CA
    Posts
    346
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well make sure that your web space can handle PHP...and also make sure that the web page(s) you're going to display the counter in has a .php extension.

  10. #10
    SitePoint Member
    Join Date
    Feb 2003
    Location
    virginia
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It does. Can you explain the "CHANGES REQUIRED" to me? They look cryptic :\ i.e give me examples of what to put in it.

  11. #11
    SitePoint Addict
    Join Date
    Aug 2001
    Location
    Los Angeles, CA
    Posts
    346
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The script can either display a counter in images or text. The way I gave the script to you, the counter is set to display in text. The extra stuff is just if you want to use images instead of text, that's all.

    And you may want to use this to call the counter PHP file to your web page instead (if the include("counter.php") doesn't work:

    <?php require("counter.php"); ?>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •