Shoutbox

Using Awstats to make a hitcounter - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: Using Awstats to make a hitcounter (/showthread.php?tid=45138)

Using Awstats to make a hitcounter by user27089 on 05-22-2005 at 08:16 PM

Does anybody know how I can use Awstats to make a hit counter, I want it a text hit counter on my site, showing the "Hits" that I have received.

Not unique :).


RE: Using Awstats to make a hitcounter by saralk on 05-22-2005 at 08:23 PM

if your using php...

code:
<?php
$file = fopen("counter.txt", "r+");
$file++;
fwrite("counter.txt", $file);
echo $file;
?>


I haven't tested it out, but it should work.
RE: Using Awstats to make a hitcounter by RaceProUK on 05-22-2005 at 08:32 PM

I'm no PHP person, but wouldn't you have a problem with '$file++;'?


RE: Using Awstats to make a hitcounter by saralk on 05-22-2005 at 08:33 PM

that makes the number increase by one.

and $file-- make it, yes you guessed it, decrease by 1 :banana:


RE: Using Awstats to make a hitcounter by RaceProUK on 05-22-2005 at 08:36 PM

Looks more to me like (Increase file pointer by 1', or have I just too much C++?


RE: Using Awstats to make a hitcounter by saralk on 05-22-2005 at 08:38 PM

im not sure, i did that pretty quickly, i can't remember if $file = fread() will put the file contents in the var, or make it some sort of identification var. WDZ WDZ WDZ where for art thou?


RE: Using Awstats to make a hitcounter by Eljay on 05-22-2005 at 08:39 PM

nah you need to do stuff in this order

fopen with r parameter to read it
fread it
store in a variable
fclose
fopen again with w parameter to clear it and prepare it for writing
add 1 to the variable
fwrite
and then echo it

code:
<?
$counter = fopen("counter.txt", "r");
$blah = fread($counter, filesize("counter.txt"));
fclose($counter);
$counter = fopen("counter.txt", "w");
$blah = $blah + 1; //not sure if $blah++ would work, try it
fwrite($counter, $blah);
fclose($counter);
echo $blah;
?>


RE: Using Awstats to make a hitcounter by Veggie on 05-22-2005 at 08:53 PM

$blah++;
would work, it simply adds one.


RE: Using Awstats to make a hitcounter by RaceProUK on 05-23-2005 at 08:33 AM

LJ's code looks more correct to me.
You gotta remember though, I don't know any PHP.


RE: Using Awstats to make a hitcounter by Eljay on 05-23-2005 at 08:36 AM

quote:
Originally posted by Veggie
$blah++;
would work, it simply adds one.

yeh but what if the txt file contains like "1000 hits" ?

it wouldnt change to 1001 because its not all integers

EDIT: actually in this case mine wouldnt work :-o
RE: Using Awstats to make a hitcounter by Stigmata on 05-23-2005 at 08:52 AM

this works pefectly fine for me, and can go over 1000 :P

code:
<?PHP
    $file="hitscount.dat";
    $handle=fopen($file, "r+");
    $hits=fread($handle,filesize("$file"));
    $hits+=1;
    fclose($handle);
    $handle=fopen($file, "w");
    fwrite($handle, $hits);
    fclose($handle);
?>

just change hitcounter.dat to some filename and then run the page, the dat file should be created automaticly. but you will get a error on first run.
RE: Using Awstats to make a hitcounter by -dt- on 05-23-2005 at 01:20 PM

quote:
Originally posted by traxor
Does anybody know how I can use Awstats to make a hit counter, I want it a text hit counter on my site, showing the "Hits" that I have received.

Not unique :).
=/ why are you all making normal file based ones insted of ones that fetch their data from "awstats"




quote:
Originally posted by Stigmata
<?PHP
    $file="hitscount.dat";
    $handle=fopen($file, "r+");
    $hits=fread($handle,filesize("$file"));
    $hits+=1;
    fclose($handle);
    $handle=fopen($file, "w");
    fwrite($handle, $hits);
    fclose($handle);
?>

=/ why do you use r+ if your just going to read from it , you should just use r then open the file with w place your data and close it. OR use w+  read from the file then write to it and close the file like so...

code:
<?php
    $file="hitscount.dat";
    $handle=fopen($file, "w+");
    $hits=fread($handle,filesize("$file"));
    $hits++;
    fwrite($handle, $hits);
    fclose($handle);
?>


Really not the best code because of no error checking but eh...