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.
It uses the text file to store a PHP array in JSON format
Working Website here
An overview:
/*
*
* 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:
<?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));
}
}
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...
Form 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>
Form with Multiple Button:
<?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>