Visit count dont work

<?php
session_start();
$counter_name = "counter.txt";
// Check if a text file exists. If not create one and initialize it to zero.
if (!file_exists($counter_name)) {
  $f = fopen($counter_name, "w");
  fwrite($f,"0");
  fclose($f);
}
// Read the current value of our counter file
$f = fopen($counter_name,"r");
$counterVal = fread($f, filesize($counter_name));
fclose($f);
// 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); 
}
$counterVal = str_pad($counterVal, 5, "0", STR_PAD_LEFT);
$chars = preg_split('//', $counterVal);
$im = imagecreatefrompng("canvas.png");
$src1 = imagecreatefrompng("dy-otto-1.png");
$src2 = imagecreatefrompng("dy-otto-2.png");
$src3 = imagecreatefrompng("dy-otto-3.png");
$src4 = imagecreatefrompng("dy-otto-4.png");
$src5 = imagecreatefrompng("dy-otto-5.png");
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 and free from memory
header('Content-Type: image/png');
echo imagepng($im);
imagedestroy($im);
?>

view at cheesestatz . com/private/counter . php

whats wrong

What goes wrong with it? Your link gives me a 404 error, so it’s a bit hard to guess where to look.

I can’t see how you decide which image file to use for each digit. I see you generate a padded string, then split it, but I can’t see where you use each digit from $counterVal / $chars when you’re creating $src1, $src2 and so on.

Try this
http://cheesestatz.com/private/connect.php

No joy there, either. All I see is:

@Transformice_Cheesez

Try adding these two lines to the top of your script:

<?php
error_reporting(-1);  
ini_set('display_errors', '1');


Update still an error
http://cheesestatz.com/private/connect.php

Which line is line #8?

That

No that surry

Try adding this function to the top of the page:

<?php 
function fred($val){
    echo '<pre style="background-color:cyan; color:red; min-height:3em; ">';
        echo '<br>';
        print_r($val);
    echo '</pre>';
}

Then insert this line before the header();

// insert this line and change variable to inspect value
fred( $counterVal );   die;

header('Content-Type: image/png');
//

Current code:

<?php
function fred($val){
    echo '<pre style="background-color:cyan; color:red; min-height:3em; ">';
        echo '<br>';
        print_r($val);
    echo '</pre>';
}
error_reporting(-1);  
ini_set('display_errors', '1');
session_start();
$counter_name = "counter.txt";
// Check if a text file exists. If not create one and initialize it to zero.
if (!file_exists($counter_name)) {
  $f = fopen($counter_name, "w");
  fwrite($f,"0");
  fclose($f);
}
// Read the current value of our counter file
$f = fopen($counter_name,"r");
$counterVal = fread($f, filesize($counter_name));
fclose($f);
// 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); 
}
$counterVal = str_pad($counterVal, 5, "0", STR_PAD_LEFT);
$chars = preg_split('//', $counterVal);
$im = imagecreatefrompng("canvas.png");
$src1 = imagecreatefrompng("dy-otto-1.png");
$src2 = imagecreatefrompng("dy-otto-2.png");
$src3 = imagecreatefrompng("dy-otto-3.png");
$src4 = imagecreatefrompng("dy-otto-4.png");
$src5 = imagecreatefrompng("dy-otto-5.png");
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 and free from memory//
// insert this line and change variable to inspect value fred( $counterVal ); die; header('Content-Type: image/png'); //
header('Content-Type: image/png');
echo imagepng($im);
imagedestroy($im);
?>

Current error: Parse error: syntax error, unexpected ‘$f’ (T_VARIABLE) in /home/cheesest/public_html/private/connect.php on line 14

url: http://cheesestatz.com/private/connect.php

See all those

That suggests a character encoding issue.

Make sure your text editor is saving the file as UTF-8 without BOM and try.

1 Like

There are some peculiar characters in the script?

if(!isset($_SESSION[‘hasVisited’])){
 $_SESSION[‘hasVisited’]=“yes”;
 $counterVal++;
 $f = fopen($counter_name, “w”);
 fwrite($f, $counterVal);
 fclose($f);
}


Try this:

<?php
error_reporting(-1);  
ini_set('display_errors', '1');

function fred($val){
    echo '<pre style="background-color:yellow; color:red; min-height:3em; ">';
        echo '<b>DEBUG</b><br><hr>';
        print_r($val);
        echo '<br>';
    echo '</pre>';
}

session_start();
$counter_name = "counter.txt";
// Check if a text file exists. If not create one and initialize it to zero.
    if (!file_exists($counter_name)) {
      $f = fopen($counter_name, "w");
      fwrite($f,"0");
      fclose($f);
    }

// Read the current value of our counter file
    $f = fopen($counter_name,"r");
    $counterVal = fread($f, filesize($counter_name));
    fclose($f);

// 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); 
}

$counterVal = str_pad($counterVal, 5, "0", STR_PAD_LEFT);
$chars = preg_split('//', $counterVal);
$im = imagecreatefrompng("canvas.png");
$src1 = imagecreatefrompng("dy-otto-1.png");
$src2 = imagecreatefrompng("dy-otto-2.png");
$src3 = imagecreatefrompng("dy-otto-3.png");
$src4 = imagecreatefrompng("dy-otto-4.png");
$src5 = imagecreatefrompng("dy-otto-5.png");
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 and free from memory//
// insert this line and change variable to inspect value:

fred( $counterVal ); // die; 

if(false)
{
    header('Content-Type: image/png');
    echo imagepng($im);
    imagedestroy($im);
}

// fred( 'LAST LINE: ' .__LINE__ );    


http://cheesestatz.com/private/connect.php more errors

canvas.png is not in the same folder as connect.php

1 Like

I can personally fix the canvas.png but the other errors…

The other errors are probably caused by the first…

Yes, those errors are because it couldn’t open canvas.png, therefore it cannot merge the other images onto it. I still cannot see how you go from the individual digits of the page count to your $src variables.

Also I think this code

 echo imagepng($im);

should just read

 imagepng($im);
1 Like

2 posts were split to a new topic: Problem with script counter.php

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.