Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Regular Coder
    Join Date
    Oct 2002
    Location
    Milwaukee, Wisconsin
    Posts
    123
    Thanks
    1
    Thanked 0 Times in 0 Posts

    [Tutorial] Making A Hit Counter With PHP

    okay you huys first i will give youthe code to analyze and then i will break it down into peices for oyu okay???

    PHP Code:
    <?php
    $filename 
    "hits.txt";
    $fdata fopen($filename"r") or die ('Counter Error');
    $fstring fread($fdatafilesize($filename));
    echo
    "$fstring";
    fclose($fdata);

    $fdata fopen($filename"w") or die ('Counter Error');
    $fcounter $fstring 1;
    $fend fwrite($fd$fcount);
    fclose($fd);
    ?>
    okay ill start by giving little definitions

    $filename this is the exact fiole name of the file preferabbly .txt to write the counter surrent info to.

    fopen fopen is a functiuon that goes and opens a file for later use in the script, the "r" stands for read it is going to read the file that it had just opend

    fread mans that after the file is open it can read something within that file to get ready to display. in this case we are looking at the filesize of $filename

    echo"$fstring"; this is what displays the counter... go ahead fiddle with it just make sure that iut has echo"$fstring OTHER TEXT HERE";

    fclose this closes the current file notifying thet fread that that session it sone with the file

    fopen with a "w" the "W" in this case stands for write, it is going to right the file when it is told to his means.

    $fcounter this is the actually counter itsself.. this is what adds oin the number 1 to $fstring

    $fend $fend is what finally sums up the proccess so that it writes to the file the current number plus on

    then we do one mor efclose and you should bbe done! pretty easy? please email me if it isnt...stoodder@lycos.co.uk

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    Canada
    Posts
    254
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I missed what the question was?

  • #3
    New Coder
    Join Date
    Mar 2003
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Whats wrong with helping? It might stop people from posting questions when the answer is already here.

  • #4
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Well you might want to throw an flock on that file in case of simultaneous read/writes. You can also slim it down and only open the file once read the value increment it, echo it, then save it.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #5
    New Coder
    Join Date
    Mar 2003
    Posts
    49
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Or do this instead:
    PHP Code:
    <?php

    $filename 
    "hits.txt";

    $readarray file ($filename);
    $fstring trim(implode (""$readarray));
    echo 
    $fstring;

    $fdata fopen($filename"w") or die ('Counter Error');
    $fcounter $fstring 1;
    $fend fwrite($fdata$fcount);
    fclose($fd);

    ?>
    Edit: I forgot something important...
    Last edited by lord_raven; 03-16-2003 at 12:29 PM.

  • #6
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Originally posted by lord_raven
    Or do this instead:
    PHP Code:
    <?php

    $filename 
    "hits.txt";

    $readarray file ($filename);
    $fstring trim(implode (""$readarray));
    echo 
    $fstring;

    $fdata fopen($filename"w") or die ('Counter Error');
    $fcounter $fstring 1;
    $fend fwrite($fdata$fcount);
    fclose($fd);

    ?>
    Edit: I forgot something important...
    you are still opening the file twice and a simulaneous read/write to the file can cause it to crash.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #7
    New Coder
    Join Date
    Mar 2003
    Posts
    49
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, no matter what you do, unless you use MySQL, you're going to run into that problem...

  • #8
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Originally posted by lord_raven
    Well, no matter what you do, unless you use MySQL, you're going to run into that problem...
    Well, no. That's what flock is for. You lock the file before you read or write to it.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #9
    Regular Coder
    Join Date
    Jun 2002
    Location
    Canada
    Posts
    254
    Thanks
    0
    Thanked 0 Times in 0 Posts
    perhaps something like this?

    PHP Code:
    <?php
    $file  
    fopen($filename,"r") or die ('Counter Error');
    flock ($file,2);
    $counter fread($filefilesize($filename));
    $counter++;
    flock($file,3);
    fclose($file);

    echo 
    "You are visitor number $counter";

    $file  fopen($filename,"w") or die ('Counter Error');
    flock($file2); 
    $fend fwrite($file$counter); 
    flock($file3); 
    fclose($file); 
    ?>
    Last edited by pb&j; 03-18-2003 at 07:52 AM.

  • #10
    Regular Coder
    Join Date
    Oct 2002
    Location
    Milwaukee, Wisconsin
    Posts
    123
    Thanks
    1
    Thanked 0 Times in 0 Posts
    hmm can i ask you wghat the flock and fend functions do.. sounds like one ends it and one locks it but im nbot sure.. sorry im getting used to this still

  • #11
    New Coder
    Join Date
    Mar 2003
    Posts
    49
    Thanks
    0
    Thanked 0 Times in 0 Posts
    flock() sets the fopen() lock mode...

    fend in this context is a variable called "constant"

    If you were to put:
    PHP Code:
    echo fend#note: no quotes 
    at the end, the output would be something to the affect of:

    Resource id #3
    Last edited by lord_raven; 03-17-2003 at 10:45 PM.

  • #12
    Regular Coder
    Join Date
    Jun 2002
    Location
    Canada
    Posts
    254
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by stoodder
    hmm can i ask you wghat the flock and fend functions do.. sounds like one ends it and one locks it but im nbot sure.. sorry im getting used to this still
    flock will "lock" the file. that is temporarily lock access the file file while it is currently in use by another person. the 2 puts the lock on, the 3 takes it off.

    i forgot the $ infront of the fend so i edited the post and you now can see it is simply a variable to be used to write the new data to the file.

  • #13
    Regular Coder
    Join Date
    Oct 2002
    Location
    Milwaukee, Wisconsin
    Posts
    123
    Thanks
    1
    Thanked 0 Times in 0 Posts
    hey thanx you guys for all of your help lol i feel like im asking to many questions i hope i dont get to annoying here i got one more question to ask but thats on the mysql dforum so yea thanx again both of you

  • #14
    New Coder
    Join Date
    Feb 2003
    Location
    dudley west mids uk
    Posts
    90
    Thanks
    0
    Thanked 0 Times in 0 Posts
    how do you change the text and font coloue etc in this counter?
    HASAAKI v.2.0 now up come and visit us at http://www.online-addicts.com/~joehall

  • #15
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,266
    Thanks
    6
    Thanked 48 Times in 48 Posts
    echo "You are visitor number $counter";

    Could be

    echo "<font color=\"#ff0000\">$counter visitors have been here</font>";


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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