Problem with script counter.php

That’s weird I don’t have that gd section when I run http://ipadress/info.php

THANK YOU SO MUCH GUYS ITS NOW WORKING, :smiley::sweat_smile:
I did not have php-gd installed once installed its working phew, could not have done this without sitepoint members help, anything i can do to support this site i will just ask.

so to fix the issue:-
install php-gd on Arch linux run command:-
pacman -Sy php-gd
then restart apache:-
then check info.php for gd enabled
open webpage and thats it working.
now i can play around and learn the code what i would like to do first is to get the counter to go up everytime i refresh

3 Likes

None taken : )
I’ve been watching this thread, but haven’t had time to jump in.

1 Like

Not sure if i need to start a new question if so i will, been play around with the script and png files and i understand how the images work, so i thought i would tidy thing up and put the files in a seperate directory so when i move thing it stopped working. i copied the counter.php
to /srv/http/phpcounter/counter.php it used to be in /srv/http/. which is the fefault folder where everything works.
so i change the index.html from:-

<img alt="Hit counter" src="counter.php" />

and changed to:-

<img alt="Hit counter" src="/srv/http/phpcounter/counter.php" />

so it should work and i am not sure why and is this a html question or php

It’s html, I think. You probably want to drop the “/srv/http/” off the beginning of the parameter, these will be virtualised by your http server.

Directories and relative paths have always caused problems especially when uploading files from Localhost to online so I have adopted this approach:

 echo '<br>Platform Constant directory: '. $_SERVER["DOCUMENT_ROOT"];

 echo '<br>Current Working directory:  '. getcwd();

// Find out how to use relative paths

// Test to see if the file exists
 if( file_exists( $fileName ) )
{
  // same path

}else file_exists( '../' .$fileName ) {
  // parent path

}else file_exists( '../../' .$fileName ) {
  // parent's parent path

}else{
  // cannot find $fileName

}//
1 Like

Ok John, i think i understand, the code, its going to check for the correct path and file, but i can’t see what it would output if the path and file do not exist and i just don’t understand, where the code goes, does it go in the index.html or counter.php. this is the bit a like the learning curve, from complete Numpty to YES I GET IT. so far i am at the numpty stage, i don’t want to be spoon fed but then again i don’t want to frustrate anyone. i have a BIG RESPECT for people who take time out to help me and others

I tried that did not work, its strange because if ichange the path of image file img=
then it seem to work

Yes, paths can be confusing. (non-technical explanation to follow)

The are file system paths eg. “C:/folder/folder/file”
And HTTP paths eg. “http://somesite/index.html

They look similar, and things get more confusing when you consider that
URIs (resource Indentifiers) look like URLs (resource Locators) but don’t need to be “real”
Some times Case matters, some times not.
Some OS use forward slash /, some backward \, sometimes either will work.

Anyway, a typical live site structure is like

/ your host account folder
. . . / “private” folder accesible to code only, not browsers
. . . / public “root” folder
. . . . . index.html
. . . . . htaccess
. . . . . robots.txt
. . . . . favicon.ico etc. etc.
. . . . . / css folder
. . . . . . . sitestyle.css
. . . . . / javascript folder
. . . . . . . sitescript.js
. . . . . / image folder
. . . . . . . logo.jpg
. . . . . / sub folder
. . . . . . . / sub sub folder etc. etc.

The easiest thing to do is to have every file that needs each other in the same folder.
But this can quickly make a folder have so many files it makes things harder instead of easier.
And if, say, “logo.jpg”, “sitestyle.css” and “sitescript.js” were to be used in sub folders and sub sub folders, there would be multiple copies of those files. If a change to a file was made, changing them all could be a problem. Even if not a problem, it gets old fast, believe me.

Organizing things helps, and how it’s done can be different, so basically it’s do it the way it needs to be done if you must, else the way that works best for you.

Absolute paths eg. “http://somesite.com/images/logo.jpg” always work but can get rather long.
“root” paths work OK and are often the preferred way eg “/images/logo.jpg”
Relative paths work, but require an understanding of where things are in relation to each other eg.
from root folder “css/sitestyle.css”,
from sub folder “…/css/sitestyle.css”,
from sub sub folder"…/…/css/sitestyle.css"

2 Likes

Hi Pullo,
I am new to the site and very new to coding PHP but i did read one of your posts on increasing the counter when pressing a button it looked a bit complicated, because i wanted my image counter to increases so as i test i changed hibards.eu counter.php with images by replacing hasVisted with refresh:-

 $_SESSION['refresh'] = "yes";

which works so what i was thinking is if i just and a button linked to this line of code would that work. PS i did try your code and it worked great, cool stuff i liked it. just could not add my image files 0.png to 9.png and was confused about the ajax link

1 Like

Not sure what you mean by this. What are you trying to accomplish?

I am trying to make my counter increases everytime i press refresh, but for some reason its now stopped working

Lol, I just looked at the page and see I need to add an extra digit to my demo.

Just remove all of the session related stuff.

I have managed to do it a different way:-

if( !isset($_SESSION['page_reload']) ) {
  $_SESSION['refresh'] = "yes";

by changing pageVisited to page_reload and refresh in the PHP script counter.PHP as shown above.

Now every time i click refresh the counter increases, which is what i wanted to try out. so for my next part instead of clicking refresh to increase the counter i would like to add a button instead, do this make sense

Maybe attach an event listener to the button that when clicked, fires off an Ajax request to the PHP script. In the PHP script increment the counter by one and return the value of the counter to your calling script. Then, insert that value into the page in the appropriate place.

Sorry i am brand new to coding and what you said goes over my head, do you have an example please

I update the online demo and if the “Increment Counter” link is selected, the $_SESSION['hasVisited'] is reset:

Online Demo (updated)

Edit:
I used PHP Strict Mode and had to modify @James_Hibbard’s “counter.php” by adding (string) to the second fwrite(…) variable - debug was not easy :frowning:

<?php
declare(strict_types=1); // PHP 7 specific
error_reporting(-1);
ini_set('display_errors', '1');
...
...
$ok = fwrite($f, (string) $counterVal);
...
...
2 Likes

Thanks John, the path change is now working, just to be clear, if the string was not added then changing the path would not work have i got that right.

and your script for declare error reporting, only shows error for paths, have i got that right?

last thing i am not sure what // copyto jpg file is used for, so what is tmp-001.jpg

once again thank you for your help and teaching me

@taz1

if the string was not added then changing the path would not work have i got that right.

The function fwrite() fails when the second parameter is not a string and PHP Strict Mode throws an error or warning. Using (string) forces the value from an integer to a string. Php 5.x is more forgiving and changes the $counterVal to a string.

   $counterVal++; // result is an integer
   $ok = fwrite($f, (string) $counterVal);


> and your script for declare error reporting, only shows error for paths, have i got that right?

Try changing the following and look for further information in the online PHP Manual :

// displays all warning and errors 
     error_reporting(-1); // does not display any errors or warning

// outputs errors and warnings to the screen
     ini_set('display_errors','1'); // '0' does not display errors to the screen.

// display error log file name and path     
      echo ini_get('error_log'); 

// reset and rename current error_log 
   ini_set('error_log', getcwd() .'/PHP_ERROR.LOG'); 

> last thing i am not sure what does > // copyto jpg file is used for, so what is tmp-001.jpg

When errors occur in “counter.php” the header(…) prevents errors from being displayed.
The following overcomes this problem.


if($DEBUG=1)
{
  // copy to jpg file
     $fff = 'tmp-001.jpg';
     $jpg = imagejpeg($im, $fff);
     echo '<img src="' .$fff .'" alt="#" />';
    
}else{
  // Output and free from memory
     header('Content-Type: image/png');
     imagepng($im);
     imagedestroy($im);    
}
2 Likes

thanks for the info, let me play around and will look at php manual, can’t get my
head around the .jpg bit yet so let me try. many thank from taz1

1 Like