Jump to content

HTML code calling PHP script not working


RPatton

Recommended Posts

I'm going to get this out of the way right up front, I am very new at this PHP stuff.

 

After a lot of research I have developed a standalone PHP file that I want to call from a HTML page. It isn't getting past my HTML code so I think I've done something there. This is my HTML code:

 

<div class="visitor-count">Number of visitors since 12-10-2016:</div>
<img alt="Hit counter" src="../javascripts/counter.php" />

 

Any help would be greatly appreciated.

 

RPatton

Link to comment
Share on other sites

The path is correct, I have checked and double checked it.

 

I don't know why I put it in that folder. It is just where my other PHP files are. :-)

 

<div class="visitor-count">Number of visitors since 12-10-2016:</div>
<img alt="Hit counter" src="../javascripts/counter.php" />

 

Of this code, the only thing that displays when I run it is:

Number of visitors since 12-10-2016:

Hit counter

 

I put this code (echo "This is a test!";) at the start of my PHP file just to see if if was even getting that far and it wasn't.

 

I also tried putting the counter.php file in the same folder as the HTML file and not using a path just src="counter.php" and that didn't work either.

Edited by RPatton
Link to comment
Share on other sites

It is a path to a bunch of number images. I could be wrong (again I'm very new at this) but I don't think it is getting to the PHP file or I would be getting an error from the PHP.

 

This is my PHP code and the paths are correct. Thanks for all your input.

 

<?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 the 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, 6, "0", STR_PAD_LEFT);
$chars = preg_split('//', $counterVal);
$im = imagecreatefrompng("../images/counter/canvas.png"); // Transparent canvas that my numbers will sit on top of
$src1 = imagecreatefrompng("../images/counter/$chars[1].png");
$src2 = imagecreatefrompng("../images/counter/$chars[2].png");
$src3 = imagecreatefrompng("../images/counter/$chars[3].png");
$src4 = imagecreatefrompng("../images/counter/$chars[4].png");
$src5 = imagecreatefrompng("../images/counter/$chars[5].png");
$src6 = imagecreatefrompng("../images/counter/$chars[6].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);
imagecopymerge($im, $src5, 300, 0, 0, 0, 56, 75, 100);
// Output and free from memory
header('Content-Type: image/png');
echo imagepng($im);
imagedestroy($im);
?>

Link to comment
Share on other sites

The issue is here:

echo imagepng($im);

You should not use echo, imagepng() outputs the image on its own. When you use echo, it adds an extra "1" to the output, therefore breaking the image.

Link to comment
Share on other sites

OK, I just tried it without the echo and it still doesn't work. :(

 

Is my HTML code correct? Am I calling the PHP file properly? I can't shake the feeling that the problem lies in the way I am calling the PHP file. :facepalm:

 

I created a test page to try and debug this and here is my entire basic HTML test page:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Hit Counter</title>
</head>

<body>
<div class="visitor-count">Number of visitors since 12-10-2016:</div>
<img alt="Hit counter" src="counter.php" />
</body>

</html>

Edited by RPatton
Link to comment
Share on other sites

If the PHP returns a valid image then that markup will work. You can test it by just going directly to the PHP page and seeing if it shows you an image. You should also use your browser's developer tools to check out the request, including the response headers from the server and check the beginning and end of the response body to see if there is any extra spacing or error messages. If you even do so much as have an extra space or newline character at the beginning of the PHP output it will corrupt the image. For scripts like that which don't return text output you should also redirect all error messages to an error log that you can check.

 

ini_set('error_log', __DIR__ . DIRECTORY_SEPARATOR . 'error.log');
ini_set('log_errors', 1);
error_reporting(E_ALL);
Link to comment
Share on other sites

The counter.txt, counter.php and html file should be in the same directory, the images folder is outside this counter directory, in the 'images' directory there is a subdirectory 'counter' this should have .png images 0-9 (0.png, 1.png, 2.png and so on...) with canvas.png image.

Link to comment
Share on other sites

Are the gd library extensions enabled? Usually your local server would have link to display this php information or check by using phpinfo() within a new php file, you should at least be getting an error or blank image box/alt text showing. There should be no spacing or line breaks before the starting <?php tag, I suppose you have correctly scaled the images and canvas image (note you need wider canvas image since it apparently look as though you added a sixth digit image).

 

Also what editor are you using?

Link to comment
Share on other sites

If there's an error in PHP, PHP will tell you. That's the point of setting up the error log and setting error reporting to the highest level. There's no reason to guess about what the error is or whether the paths are correct. Use an error log, that's the point of it.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...