计数器增量++错误的数字

时间:2016-12-13 12:34:09

标签: php

<?php
   $counter = 'www.mywebsite.com/counter/counter.txt';
   $download = 'www.mywebsite.com/file/1.pdf';

   $number = file_get_contents($counter); // read count file 
   $number++; // increment count by 1 
   $fh = fopen($counter, 'w'); // open count file for writing 
   fwrite($fh, $number); // write new count to count file 
   fclose($fh); // close count file 
   header("Location: $download"); // get download 

?>

为什么数字计数器在ex:5,999之后重启5000?而不是6,000 ???

1 个答案:

答案 0 :(得分:0)

<?php
    $counter = 'www.mywebsite.com/counter/counter.txt';
    $download = 'www.mywebsite.com/file/1.pdf';

    $number = intval(file_get_contents($counter)); // read count file 
    $number++; // increment count by 1 
    $fh = fopen($counter, 'w'); // open count file for writing 
    fwrite($fh, $number); // write new count to count file 
    fclose($fh); // close count file 
    header("Location: $download"); // get download 

&GT;

!!! intval()...已修复。