存储服务器上.txt/.php文件内部的点击


Store clicks inside .txt/.php file on the server

我知道你们中的大多数人都会说,在.txt.php文件中保存点击比在DB中慢,我同意,但我要求。。。

我已经有了一个脚本,它将单个表单的点击存储在.txt文件中。问题是,我需要将该脚本用于多个表单,并将点击量存储在同一个.txt(或.php)文件中,并返回每个按钮的点击次数。这是我做不到的。

如果你对我的问题有解决方案,并想发布答案,请添加一些解释,这样我就可以理解你为什么/如何这样做,这样我不会再问同样的问题了。谢谢

以下是我目前所拥有的:

HMTL:

<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
    <input type="submit" value="click me!" name="clicks">
</form>
<div>Click Count: <?php echo getClickCount(); ?></div>

PHP:

if( isset($_POST['clicks']) ) { 
    incrementClickCount();
}
function getClickCount()
{
    return (int)file_get_contents("clickit.txt");
}
function incrementClickCount()
{
    $count = getClickCount() + 1;
    file_put_contents("clickit.txt", $count);
}

更新我愿意为此使用json(据我所知,这是可以做到的),但你必须忍受我,因为我从未使用过json之前,即使读写很容易,第一次也是第一次时间

将单个"html表单"中的"点击次数"存储在文本文件中。它经过了测试。

添加了一个使用带有多个按钮的表单计算点击次数的示例

它使用文本文件以JSON格式存储PHP数组

此处的工作网站

概述:

/*
 *
 * We need to store the form 'click' counts in an array:
 * i.e.
 *   array( 'form1' =>  click count for Form1,
 *          'form2' =>  click count for form2
 *          ...
 *        );
 *
 * We need a 'key' to identify the 'current' form that we are counting the clicks of.
 *   I propose that it is based on the 'url' that caused the form to be actioned.
 *
 * The idea is to create an array of 'click counts' and 'serialize' it to and from a file.
 *
 * Performance may be an issue for large numbers of forms.   
 *  
 */

我创建了一个类来"照顾"文本文件。

单击计数.php:

<?php
/**
 * All the code to maintain the 'Form Click Counts' in a text file...
 *
 * The text file is really an 'array' that is keyed on a 'Text String'.
 *
 * The filename format will work on 'windows' and 'unix'.
 *
 * It is designed to work reliably rather than be 'cheap to run'.
 *
 * @author rfv
 */
class ClickCount {
    const CLICK_COUNT_FILE = 'FormClickCounts.txt';
    protected $clickCounts = array();

    public function __construct()
    {
        $this->loadFile();
    }

    /**
     * increment the click count for the formId
     *
     * @param string $textId - gets converted to a 'ButtonId'
     */
    public function incClickCount($textId)
    {
        $clickId = $this->TextIdlToClickId($textId);
        if (isset($this->clickCounts[$clickId])) {
            $this->clickCounts[$clickId]['click']++;
        }
        else {
            $this->clickCounts[$clickId]['textId'] = $textId;
            $this->clickCounts[$clickId]['click'] = 1;
        }
        $this->saveFile();
    }
    /**
     * Return the number of 'clicks' for a particular form.
     *
     * @param type $clickId
     * @return int clickCounts
     */
    public function getClickCount($textId)
    {
        $clickId = $this->TextIdlToClickId($textId);
        if (isset($this->clickCounts[$clickId])) {
            return $this->clickCounts[$clickId]['click'];
        }
        else {
            return 0;
        }
    }
    /**
     *
     * @return array the 'click counts' array
     */
    public function getAllClickCounts()
    {
        return $this->clickCounts;
    }
    /**
     * The file holds a PHP array stored in JSON format
     */
    public function loadFile()
    {
        $filePath = __DIR__ .'/'. self::CLICK_COUNT_FILE;
        if (!file_exists($filePath)) {
            touch($filePath);
            $this->saveFile();
        }
        $this->clickCounts = json_decode(file_get_contents($filePath), true);
    }
    /**
     * save a PHP array, in JSON format, in a file
     */
    public function saveFile()
    {
        $filePath = __DIR__ .'/'. self::CLICK_COUNT_FILE;
        file_put_contents($filePath, json_encode($this->clickCounts));
    }
    /**
     * 'normalise' a 'form action' to a string that can be used as an array key
     * @param type $textId
     * @return string
     */
    public function TextIdlToClickId($textId)
    {
        return bin2hex(crc32($textId));
    }
}

运行这个的"index.php"文件。。。

<?php // https://stackoverflow.com/questions/28912960/store-clicks-inside-txt-php-file-on-the-server
include __DIR__ .'/ClickCount.php';
/*
 * This is, to be generous, 'proof of concept' of:
 *
 *   1) Storing click counts for individual forms in a text file
 *   2) Easily processing the data later
 */
/*
 *
 * We need to store the form 'click' counts in an array:
 * i.e.
 *   array( 'form1' =>  click count for Form1,
 *          'form2' =>  click count for form2
 *          ...
 *        );
 *
 * o We need a 'key' to identify the 'current' form that we are counting the clicks of.
 *   I propose that it is based on the 'url' that caused the form to be actioned.
 *
 * The rest i will make up as i go along...
 */
?>
<!DOCTYPE html>
<head>
    <title>store-clicks-inside-txt-php-file-on-the-server</title>
</head>
<body>
    <h2>select form...</h2>
    <p><a href="/form1.php">Form 1</a></p>
    <p><a href="/form2.php">Form 2</a></p>
    <h2>Current Counts</h2>
    <pre>
    <?php $theCounts = new ClickCount(); ?>
    <?php print_r($theCounts->getAllClickCounts()); ?>
    </pre>
</body>

计算"点击"的两个表单文件。。。

表格1

<?php
include __DIR__ .'/ClickCount.php';
// change this for your system...
define ('HOME', '/index.php');
// this looks after the 'click counts' text file
$clickCounts = new ClickCount();
if (isset($_POST['cancel'])) {
    header("location: ". HOME);
    exit;
}
// increment the count for the current form
if (isset($_POST['clicks'])) {
    $clickCounts->incClickCount($_SERVER['PHP_SELF']);
}
?>
<h2>Form 1</h2>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
    <input type="submit" value="click me!" name="clicks">
    <input type="submit" value="cancel" name="cancel">
</form>
<div>Click Count: <?php echo $clickCounts->getClickCount($_SERVER['PHP_SELF']); ?></div>

带多个按钮的表单:

<?php
include __DIR__ .'/ClickCount.php';
// change this for your system...
define ('HOME', '/clickcount/index.php');
// this looks after the 'click counts' text file
$clickCounts = new ClickCount();
if (isset($_POST['cancel'])) {
    header("location: ". HOME);
    exit;
}
// increment the count for the current form
if (isset($_POST['clicks'])) {
    $clickCounts->incClickCount($_POST['clicks']);
}
?>
<h2>Multiple Buttons on the Form - Count the Clicks</h2>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
    <br /><label for="clicks1">1) Click Me</label><input type="submit" value="click me - 1!" id="clicks1" name="clicks">
    <br /><label for="clicks1">2) Click Me</label><input type="submit" value="click me - 2!" id="clicks2" name="clicks">
    <br /><label for="clicks1">3) Click Me</label><input type="submit" value="click me - 3!" id="clicks3" name="clicks">
    <br /><label for="clicks1">4) Click Me</label><input type="submit" value="click me - 4!" id="clicks4" name="clicks">
    <br /><br /><input type="submit" value="cancel" name="cancel">
</form>
<div>Click Counts:
       <?php foreach ($clickCounts->getAllClickCounts() as $counts): ?>
           <?php if (strpos($counts['url'], 'click me -') !== false): ?>
             <?php echo '<br />Counts for: ', $counts['url'], ' are: ', $counts['click']; ?>
           <?php endif ?>
       <?php endforeach ?>
</div>

检查此方法(未测试)。

$my_file = 'clickit.txt';
//Read the file
$handle = file_get_contents($my_file);
//Add click
$data = $handle+1;
//Write the file
file_put_contents($my_file,$data);