I copied some scripts from the above posts and made the following:
Online Demo [with ource code]
The default is `$DEBUG``` which saves a random number to “counter.txt” and creates a temporary jpg file to display the results.
Selecting Use: header(“Content-Type: image/png”) does not use `$DEBUG```
I find it is a lot easier to modify working code:)
###Source:
<?php
declare(strict_types=1); // PHP 7 specific
error_reporting(-1); ini_set('display_errors', '1');
$title = 'Problem with PHP script counter';
$sp = 'https://www.sitepoint.com/community/t/problem-with-script-counter-php/214562/49';
define('jj', '<br>');
//=====================================================
function fred($val)
{
echo '<pre>';
var_dump($val);
echo '</pre>';
}
// START
session_start();
$__DEBUG__ = ! isset( $_GET['debug'] ); // Change this to false when you want to stop the extra echos
$counter_name = "counter.txt";
// Check if a text file exists. If not create one and initialize it to zero.
if ( ! file_exists($counter_name) )
{
// echo $__DEBUG__ ? "Counter file did not exist" : NULL; // ** DEBUG
$f = fopen($counter_name, "w");
fwrite($f,"0");
fclose($f);
}
// DEBUG - create large number
if($__DEBUG__)
{
$f = fopen($counter_name, "w");
fwrite($f, sprintf('%d', mt_rand(1,12345)) );
fclose($f);
}
// GET/SET COUNTER
// Read the current value of our counter file
$f = fopen($counter_name,"r");
$counterVal = fread($f, filesize($counter_name));
fclose($f);
// if ($__DEBUG__) echo "Counter value is " . $counterVal; // ** DEBUG
// INCREMENT COUNTER, MAYBE CREATE
// Has visitor been counted in this session?
// If not, increase counter value by one
if(!isset($_SESSION['hasVisited'])){
$_SESSION['hasVisited']="yes";
$counterVal++;
$f = fopen($counter_name, "w");
fwrite($f, $counterVal);
fclose($f);
// echo $__DEBUG__ ? "Incremented counter" : NULL; // ** DEBUG
}
// GET COUNTER Values
$counterVal = str_pad($counterVal, 5, "0", STR_PAD_LEFT);
$chars = str_split($counterVal);
// $im = imagecreatefrompng("canvas2.png"); // 300 x 75
// CREATE Associated images for each counter item
$src1 = imagecreatefrompng("dy-otto-" .$chars[0] . ".png"); // 56 x 75
$src2 = imagecreatefrompng("dy-otto-" .$chars[1] . ".png");
$src3 = imagecreatefrompng("dy-otto-" .$chars[2] . ".png");
$src4 = imagecreatefrompng("dy-otto-" .$chars[3] . ".png");
$src5 = imagecreatefrompng("dy-otto-" .$chars[4] . ".png");
// BUILD Image
$im = imagecreate(320,77); // 300 x 75
imagecopymerge($im, $src1, 0, 0, 0, 0, 56, 75, 100);
imagecopymerge($im, $src2, 60, 0, 0, 0, 56, 75, 100);
imagecopymerge($im, $src3, 120, 0, 0, 0, 56, 75, 100);
imagecopymerge($im, $src4, 180, 0, 0, 0, 56, 75, 100);
imagecopymerge($im, $src5, 240, 0, 0, 0, 56, 75, 100);
# OUTPUT Results
if( $__DEBUG__ )
{
$fff = 'tmp-001.jpg';
$jpg = imagejpeg($im, $fff);
imagedestroy($im);
echo $page = '
<!doctype html>
<html>
<head>
<title> $title </title>
<style>
html,
body {margin:0; padding:0; background-color:black; color:snow;}
#top {margin:0; padding:0; height:2em; background-color:#000;}
.bd1 {border:solid 1px #123;}
.bgs {background-color:snow; color:#333;}
.clb {clear:both;}
.fgc {color:cyan;}
.flr {float:right;}
.mga {margin:0 auto;} .mg2 {margin:2em auto;}
.p42 {padding:0.42em}
.ooo {margin:0; padding:0;}
.tac {text-align:center}
.w88 {width:88%;}
</style>
</head>
<body>
<div id="top">
<em class="flr"> <a href="' .$sp .'"><em class="fgc">SitePoint Forum</em></a> </em>
<h1>' .$title .'</h1>
<hr class="clb" />
</div>';
echo '<div class="clb mg2 bgs"><br>';
if ($jpg) // file_exists($fileName)
{
echo '<dl class="clb w88 mg2 bgs">';
echo '<dt><b>File: </b> counter.txt content ==> ' . $counterVal .'<dt>';
echo '<dd>';
echo '<img src="' .$fff .'" alt="counter" />';
echo '</dd>';
echo '<dd> </dd>';
echo '<dd> </dd>';
echo '<dt"> <a href="index.php?debug"> Use: header("Content-Type: image/png") </a> </dt>';
echo '<dd> </dd>';
echo '</dl>';
}
// Show Source
echo '<div id="src" class="w88 mga p42 bd1">';
echo '<b>Source: </b><br>';
highlight_file(__FILE__);
echo '</div>';
echo '</div></body></html>';
}else{
// Output and free from memory
header('Content-Type: image/png');
imagepng($im);
}