Results 1 to 6 of 6

Thread: Can you explain this?

  1. #1

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Can you explain this?

    Okay, i found this good php script:

    PHP Code:
    <?php
    $countfilename 
    "VisitorCount.txt";
    $whattoread = @fopen($countfilename"r") or die("Error opening count file");
    $contents fread($whattoreadfilesize($countfilename));
    fclose($whattoread);

    $contents = ($contents 1);

    $whattoread = @fopen($countfilename"w") or die("Error opening count file1");
    @
    fwrite($whattoread$contents) or die("Error with addition");

    fclose($whattoread);
    echo 
    $contents;
    ?>
    works like a charm..great job..what im wondering is if i can have an explanation for what it all does?

    you dont have to explain like in depth, just like $countfilename, and all those and there arguments...anything you see fit..

    thanks alot


    **edit
    im beginning to understand alot, this isnt so hard, at all!!

    heres what ive figured out in the last 5 minutes:

    Everything with a $ is a string, not a fixed variable(what i mean is, to open a file i dont have to type $whattoread =, it can be $readfile =

    $whattoread = @fopen($filename, "r") or die("Error opening count file");
    means that it should "r"ead the file, on error say ("Error opening count file")--dont really understand what die does though

    and that you can add to strings: echo $contents, " Visitors"

    other help = good
    Last edited by visualAd; May 2nd, 2005 at 06:05 PM. Reason: bizarre request :\

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Can you explain this?

    It looks to me like a hit counter. All variables are prefixed with a $ symbol.

    Line 1 assigns the string VisitorCount.txt to the variable $countfilename

    Line 2 opens the file in $countfilename for reading and assigns the returned resource to the variable $whattoread

    Line 3 assigns the content of the file in $whattoread to the varaible $contents as string.

    Line 4 closes the file in $whattoread

    Line 5 type casts the variable $contents to an integer and adds one to it

    Line 6 again opens the file in the variable $countfilename but this time for wirting and assigns its resource to the variable $whattoread

    Line 7 the contents of the variable $contents is type casted to a string and written to the file

    Line 8 the file in in $whattoread it closed

    Line 9 the contents of the variable $contents are sent to the output stream. Usually the browser.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Can you explain this?

    cool, great post and very helpful

    Can you explain to me how id save to a website the counter? maybe thru ftp or something?

  4. #4
    Frenzied Member numtel's Avatar
    Join Date
    Apr 2000
    Location
    CA
    Posts
    1,163

    Re: Can you explain this?

    You would put that code posted above into a php file then make another file VisitorCount.txt with the contents "0" and chmod it to 777. If you then open the php file through the webserver it will say 1 hit. That code could be much easier though:
    PHP Code:

    if(!function_exists('file_put_contents')) {
      function 
    file_put_contents($filename$data$file_append false) {
       
    $fp fopen($filename, (!$file_append 'w+' 'a+'));
       if(!
    $fp) {
         
    trigger_error('file_put_contents cannot write in file.'E_USER_ERROR);
         return;
       }
       
    fputs($fp$data);
       
    fclose($fp);
      }



    $Counter=file_get_contents("VisitorCount.txt");
    $Counter++;
    file_put_contents("VisitorCount.txt",$Counter);
    echo 
    $Counter
    I guess it's not that much shorter since you have to declare the function unless you're using PHP 5 but I have that function in must of my includes anyways. Either way it should work.
    Last edited by numtel; May 2nd, 2005 at 11:14 PM.

  5. #5

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Can you explain this?

    i honestly understood none of your code...could you explain?

    btw, i understand how to use the code, lol thats the easy part
    You would put that code posted above into a php file then make another file VisitorCount.txt with the contents "0"

  6. #6

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Can you explain this?

    okay, i uploaded that file to a server(the one in my first post) and im getting a write error. I assume its because it does not have access to the file..any ideas?

Posting Permissions

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



Click Here to Expand Forum to Full Width