RPatton Posted December 15, 2016 Share Posted December 15, 2016 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 More sharing options...
Ingolme Posted December 15, 2016 Share Posted December 15, 2016 Either the path to the file is wrong or the file is not returning valid image data. Why are you putting a PHP file in a folder called "javascripts"? Link to comment Share on other sites More sharing options...
RPatton Posted December 15, 2016 Author Share Posted December 15, 2016 (edited) 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 December 15, 2016 by RPatton Link to comment Share on other sites More sharing options...
Ingolme Posted December 15, 2016 Share Posted December 15, 2016 The image tag will not show anything if PHP is not outputting image data. If you just print text it won't work. What is your PHP code? Link to comment Share on other sites More sharing options...
dsonesuk Posted December 15, 2016 Share Posted December 15, 2016 Well unless the text is the path to a actual image, it won't! If yoh want to show text place in for example a paragraph element 'p' or 'div' element. Link to comment Share on other sites More sharing options...
RPatton Posted December 15, 2016 Author Share Posted December 15, 2016 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. <?phpsession_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 oneif(!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 memoryheader('Content-Type: image/png');echo imagepng($im);imagedestroy($im);?> Link to comment Share on other sites More sharing options...
Ingolme Posted December 15, 2016 Share Posted December 15, 2016 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 More sharing options...
RPatton Posted December 15, 2016 Author Share Posted December 15, 2016 (edited) 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. 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 December 16, 2016 by RPatton Link to comment Share on other sites More sharing options...
justsomeguy Posted December 16, 2016 Share Posted December 16, 2016 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 More sharing options...
dsonesuk Posted December 16, 2016 Share Posted December 16, 2016 The problem is this imagecopymerge($im, $src5, 300, 0, 0, 0, 56, 75, 100); It should be imagecopymerge($im, $src6, 300, 0, 0, 0, 56, 75, 100); $src6 Link to comment Share on other sites More sharing options...
dsonesuk Posted December 16, 2016 Share Posted December 16, 2016 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 More sharing options...
RPatton Posted December 16, 2016 Author Share Posted December 16, 2016 OK, I moved everything to the same directory, took out all the relative paths, fixed the $src6, and it still doesn't work. I'm about ready to pull my hair out. Link to comment Share on other sites More sharing options...
dsonesuk Posted December 16, 2016 Share Posted December 16, 2016 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 More sharing options...
justsomeguy Posted December 16, 2016 Share Posted December 16, 2016 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now