www.webdeveloper.com
Results 1 to 3 of 3

Thread: backup the counter into a file

  1. #1
    Join Date
    Feb 2011
    Posts
    17

    backup the counter into a file

    hello everyone,
    Atm I'm trying to make a counter reseter work, but no luck.
    Here is content of the reset_counter.php:
    PHP Code:
    <?php 
    $reset 
    "0"
    $file "counter.txt"
    $handle fopen ($file"r"); 
    $contents fread ($handlefilesize ($file)); 
    fclose ($handle); 
    print 
    "Total number of views: <b>$contents</b>"
    $date date('h:i:s'); 
    $translate 'echo $date | tr : .'
    $command 'echo $contents > backup/counter_$translate.txt'
    system("$translate"); 
    exec("$command"); 
    $fp fopen($file,"w"); 
    fwrite($fp,$reset); 
    fclose($fp); 
    ?>
    I tried to allow the safe exec dir in htaccess, but then it gave me error 500.
    Any straight answers will be appreciated.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,147
    It would, in my opinion, be both simpler and safer to use PHP's file functions instead of system() commands.
    PHP Code:
    <?php
    $reset 
    "0";
    $file "counter.txt";
    $contents trim(file_get_contents($file));
    print 
    "Total number of views: <b>$contents</b>";
    $date date('h.i.s'); // no need to use ":" if you want "."
    file_put_contents("backup/counter_$date.txt"$contents);
    file_put_contents($file$reset);
    ?>
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Feb 2011
    Posts
    17
    oh yeah, I found out, those system and exec calls can make a way to hackers to do something nasty.
    So I looked for alternatives, like this:
    PHP Code:
    <?php 
    $reset 
    "0"
    $file "counter.txt"
    $handle fopen ($file"r"); 
    $contents fread ($handlefilesize ($file)); 
    fclose ($handle); 
    print 
    "Total number of views: <b>$contents</b>"
    $date date('Y.m.d-h.i.s');
    copy("counter.txt","backup/counter_$date.txt");
    $fp fopen($file,"w"); 
    fwrite($fp,$reset); 
    fclose($fp); 
    ?>
    but yours is smaller, so thanks for it

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles