dsyo9700 2013-05-27 21:51
浏览 12
已采纳

PHP中的是/否计数器

I wrote this counter which keeps track of yes/no for a website and it works decently well, the problem is somehow the files get messed up while writing. For example: it'll go from 126 to 27. The script gets called from an iOS app I wrote so most likely there are multiple connections modifying the file at the same time and I think this is what is causing the problem. I am not really a PHP guy so I was hoping for some insight on what could make the code a little better and handle multiple simultaneous connects.

<?php

        $yes_file = 'yes.txt';
        $no_file  = 'no.txt';

        $yes_count = file_get_contents($yes_file);
        $no_count = file_get_contents($no_file);

        if ($_GET['result'])
        {
                if( strcmp($_GET['result'], "YES") ) {
                        $no_count+=1;
                        file_put_contents($no_file, $no_count);
                }
                else {
                        $yes_count+=1;
                        file_put_contents($yes_file, $yes_count);
                }
        }

        $total = $yes_count + $no_count;
        echo "{\"yescount\":" . $yes_count.",";
        echo "\"nocount\":" . $no_count.",";
        echo "\"total\":" . $total."}";

?>

Thanks!

  • 写回答

2条回答 默认 最新

  • dongpixi2648 2013-05-28 02:27
    关注

    This should be more efficient.

    FYI, a database puts a write lock on the row/table whilst incrementing, which is the same as what I've done below, so a database is not the solution - the solution is a write lock (via a database or via PHP). You could use flock, but I find that messy so I've done it just with a temporary file.

    The only issue with my code here is that if the server crashes in the middle of this script then the write lock will stay in place (MySQL has this issue too sometimes). I usually get around this by writing the time() in the file and checking that it is not older than an an hour or something. But in your case it's probably unnecessary.

    <?php
    
    // Your variables
    $yes_file = 'yes.txt';
    $no_file  = 'no.txt';
    
    if (isset($_GET['result']))
    {
    // Write lock
    while(file_exists('temporaryfile')) usleep(100000);
    file_put_contents('temporaryfile','1');
    
    $yes_count = (int)file_get_contents($yes_file);
    $no_count = (int)file_get_contents($no_file);
    
    // Increment
    if ($_GET['result']=='YES')
        {
        $yes_count++;
        file_put_contents($yes_file, $yes_count);
        }
    else
        {
        $no_count++;
        file_put_contents($no_file, $no_count);
        }
    
    // Unlock
    unlink('temporaryfile');
    }
    else // No need for any lock so just get the vars
    {
    $yes_count = (int)file_get_contents($yes_file);
    $no_count = (int)file_get_contents($no_file);
    }
    
    $total = $yes_count + $no_count;
    echo "{\"yescount\":$yes_count,
    \"nocount\":$no_count,
    \"total\":$total}";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件
  • ¥15 使用ESP8266连接阿里云出现问题