Does Synchronization work properly in file handling in php?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tushar deshpande
    New Member
    • Jul 2007
    • 2

    Does Synchronization work properly in file handling in php?

    hello,
    i have prepared a php file for visitcount. that creates a text file per

    day. as soon as any user visits that page ,a counter is incremented. but on

    my site there is lots of trafic. that's why counter again starts with zero

    . and file also written to null.b'coz multiple users(abt 1 to 10 lakh) can

    visit page at same time.

    so i want to know if code is right then what is the problem?
    how can we solve this proble? is there any locking file mechanism in php?
    code :[code=php]
    $count_path = "../count_visit/".date("d-m-Y").".txt";

    if(file_exists( $count_path))
    {
    $handle = fopen($count_pa th, 'r');
    $read_count= fread($handle, filesize($count _path));


    fclose($handle) ;
    /* or used $read_count = file_get_conten ts($count_path) ;
    */
    if($read_count <> '' )
    {
    $inc_count = (int)$read_coun t + 1;
    $handle = fopen($count_pa th, 'w');
    fwrite($handle, $inc_count);
    fclose($handle) ;
    }
    }
    else
    {
    // executes when any user visits first time
    $handle = fopen($count_pa th, 'w');
    fwrite($handle, '1' );
    fclose($handle) ;
    }[/code]

    Plz solve this problem.
    Last edited by pbmods; Jul 26 '07, 02:09 PM. Reason: Added CODE tags.
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, Tushar.

    Please use CODE tags when posting source code. See the REPLY GUIDELINES on the right side of the page next time you post.

    Comment

    • ak1dnar
      Recognized Expert Top Contributor
      • Jan 2007
      • 1584

      #3
      Moving thread from Php Articles section to the Forum.
      -ajaxrand

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Heya, Tushar.

        Check out flock().

        Comment

        • kovik
          Recognized Expert Top Contributor
          • Jun 2007
          • 1044

          #5
          File-locking may give you unexpected results when multiple visitors come, as some of them will be disenfranchised .

          If you have access to a database, you could do this very easily there, as it accounts for multiple queries within itself.

          Comment

          Working...