将点击存储在服务器上的.txt / .php文件中 [英] Store clicks inside .txt/.php file on the server
问题描述
我知道大多数人会说,在 .txt 或 .php 文件中保存点击比在数据库中更慢,我同意,但我需要this ...
I know that most of you will say that saving clicks inside a .txt or .php file is slower than in a DB, and I agree, but I require this...
我已经有一个脚本存储 .txt 文件中单个表单的点击。事情是,我需要使用该脚本的多个表单,并将点击存储在同一个 .txt(或.php)文件中,并回应每个按钮的点击次数。这是我无法实现的。
I already have a script that stores the clicks of a single form inside a .txt file. The thing is that I need to use that script for multiple forms and store the clicks in the same .txt (or .php) file and echo the number of clicks on each button. That's what I can't achieve.
如果您有我的问题的解决方案,并想发布答案,请添加一些解释,为什么/你是怎么做的,所以我不会再问同样的问题了。感谢
这是我到目前为止:
HMTL: strong>
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
之前,即使它很容易读/写,第一次是第一
time ...
Update I am willing to use json for this (as I understand it can be done) but you will have to bear with me cause I never used json before and even if it's easy to read/write, first time is first time...
推荐答案
在文本文件中。它经过测试。
Store 'click counts' from individual 'html forms' in a text file. It is tested.
添加了使用包含多个按钮的表单计数点击的示例。
Added an example of Counting Clicks using a form with Multiple Buttons.
概述:
/*
*
* 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.
*
*/
我创建了一个文本文件。
I created a class that 'looks after' the text file.
ClickCount.php:
ClickCount.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'文件。 ..
The 'index.php' file that runs this...
<?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>
点击的两个表单文件被计数...
The two form files that 'clicks' are counted...
表单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>
这篇关于将点击存储在服务器上的.txt / .php文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!