【问题标题】:Store clicks inside .txt/.php file on the server将点击存储在服务器上的 .txt/.php 文件中
【发布时间】:2015-05-08 21:28:24
【问题描述】:

我知道你们中的大多数人会说在 .txt.php 文件中保存点击比在数据库中慢,我同意,但我需要这……

我已经有一个脚本,它将单个表单的点击存储在 .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 之前,即使它很容易读/写,第一次是第一次 时间……

【问题讨论】:

  • 您的问题到底是什么?为什么必须写入文件?你知道这个解决方案的问题吗? 请添加一些解释,以便我能理解您想要什么,这样我就不会再问同样的问题了。
  • @RoToRa 你好,我的问题是我不能将上面的代码用于多个按钮(好吧,我可以,但它确实增加了 clickit.txt 文件中所有按钮的值,而不是单独,因此所有按钮将返回相同的点击次数。)。就像我说的,我知道它没有数据库那么快,也没有那么安全,但我并不担心。对于希望通过 FTP 访问它并每周将其插入统计程序一次的客户,我需要它。 :) 这能回答你的问题吗?
  • 您可以使用 json 来存储按钮的名称和点击次数。当您使用 json_decode 时,您将拥有带有名称/点击的 PHP 数组/对象。然后,您可以仅增加所需按钮的点击次数。问题是,如果您有多个用户同时点击,您将因为您使用的一个文件而丢失数据。这就是为什么 DB 是此类事务操作的首选解决方案
  • 可以用CSV格式吗?
  • @smokeyPHP 我认为在此过程中我尝试使用逗号尝试单独的值但是(由于我缺乏 php 知识)我无法实现它......

标签: php html count store


【解决方案1】:

检查此方法(未测试)。

$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);

【讨论】:

  • 我已经用两个按钮对其进行了测试,但它没有按预期工作(就像我的版本一样,它确实增加了 clickit.txt 文件中的值,但它对两个按钮都这样做,而不是单独这样做,因此两个按钮将返回相同的点击次数。)
  • 您使用的php版本是什么?你坚持使用 fopen、fwrite 和 fclose 吗?
【解决方案2】:

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

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

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

Working Website here

概述:

/*
 *
 * 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>

【讨论】:

  • 嘿,我很快就会测试这个,感谢一切和解释,但问题是,我一直在寻找一种更简单的方法,使用这个解决方案,如果我有 18 个按钮,我必须复制 Form.php 18 次,这不是一个好主意...无论如何,我会在测试后回复您:)
  • 嗯,这看起来很有希望,我会研究它并根据我的需要进行修改。谢谢,我会标记它,因为它确实回答了我的问题:) 祝你有美好的一天。
  • @Alin,很高兴能帮上忙。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-08
  • 1970-01-01
  • 2011-03-17
  • 2015-07-01
  • 1970-01-01
  • 2012-10-12
相关资源
最近更新 更多