0

I am trying to determine why an Image Verification won't display images prior to a press release being issued pointing to the website.

The related image.php file is showing the alt text and a check on Console shows the error "Failed to load resource: the server responded with a status of 502 (Bad Gateway) image.php".

The host has checked things on their end and assures everything is as it should be, suggesting the problem is somewhere within the php code, however the code seems to point to the images as it should, and we are lost as to what is going wrong.

The image.php code follows:

<?php if (!isset($_SESSION)) session_start(); header("(anti-spam-content-type:) image/png");

$enc_num = rand(0, 9999);
$key_num = rand(0, 24);
$hash_string = substr(md5($enc_num), $key_num, 5); // Length of String
$hash_md5 = md5($hash_string);

$_SESSION['verify'] = $hash_md5;

// Fallback
setcookie("verify", $hash_md5, time()+3600, "/");

session_write_close();

// Verification Image Background Selection

$bgs = array("../images/contact/verify/1.png","../images/contact/verify/2.png","../images/contact/verify/3.png");
$background = array_rand($bgs, 1);

// Verification Image Variables

$img_handle = imagecreatefrompng($bgs[$background]);
$text_colour = imagecolorallocate($img_handle, 108, 127, 6);
$font_size = 5;

$size_array = getimagesize($bgs[$background]);
$img_w = $size_array[0];
$img_h = $size_array[1];

$horiz = round(($img_w/2)-((strlen($hash_string)*imagefontwidth(5))/2), 1);
$vert = round(($img_h/2)-(imagefontheight($font_size)/2));

// Make the Verification Image

imagestring($img_handle, $font_size, $horiz, $vert, $hash_string, $text_colour);
imagepng($img_handle);

// Destroy the Image to keep Server Space

imagedestroy($img_handle);
halfer
  • 19,824
  • 17
  • 99
  • 186
Bobby Gavin
  • 3
  • 1
  • 7
  • ADDENDUM: The web page in question on which the non-working Image Verification is located, can be viewed [HERE](http://charityfilm.co.uk/brandint/index.html) and a working version of this can be viewed [HERE](http://www.lglab.co.uk/eko/video/index.html) – Bobby Gavin Jul 17 '17 at 21:00
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Jul 17 '17 at 21:18
  • Is there any more information in the PHP or Apache logs? – halfer Jul 17 '17 at 21:18
  • The error sounds like a proxy is not able to connect to an endpoint, but since the `image.php` is on the same server, that's unlikely. What I'd do is, if the logs angle fails, replace the `image.php` file with a ` – halfer Jul 17 '17 at 21:23
  • Thank you for the advice halfer. Regarding the PHP or Apache logs, where and how can I obtain these? I'm not the developer of the website and left trying to sort this out before the morning. I am a complete novice sadly, so do need further guidance than most, and a measure of patience for my lack of knowledge. – Bobby Gavin Jul 17 '17 at 21:42
  • I asked the host for the Apache log and obtained the following information (I tried to add to a comment as reply but it was too long) so posted as an Answer, and also had to add a space in the links as error stated "You need at least 10 reputation to post more than 2 links".... – Bobby Gavin Jul 18 '17 at 06:27
  • OK, so the logs approach has not worked - the access logs do not help and there's nothing in the error log. Please try my `readfile()` script idea above. – halfer Jul 18 '17 at 08:54

2 Answers2

0

Hmm, this is almost certainly a problem:

<?php if (!isset($_SESSION)) session_start(); header("(anti-spam-content-type:) image/png");

There are two issues here:

  • An if clause without braces should not have many items on the same line, since this makes it look like the header() is conditional when it is not;
  • The header is invalid - there is no such HTTP header as (anti-spam-content-type:)

Consider replacing it with:

<?php

if (!isset($_SESSION)) {
    session_start();
}
header("Content-Type: image/png");
halfer
  • 19,824
  • 17
  • 99
  • 186
  • Sadly no effect. I have just received word back from the original developer of the code, and they have assured it is all as it should be, and works on their server. They suggested it must be an issue with my host so I have just moments ago raised a ticket with them, and I will update any progress here. Thank you again for your appreciated on-going assistance. – Bobby Gavin Jul 18 '17 at 11:21
  • Did you try the `readfile()` approach, @Bobby? I won't ask a third time about that. – halfer Jul 18 '17 at 11:22
  • Sorry about that, I wasn't sure exactly where to add the – Bobby Gavin Jul 18 '17 at 11:26
  • Put it in a file called `image.php` and replace the existing `image.php` with it. Obviously, backup the old copy. Then upload a JPEG called `myimage.jpeg` (or whatever name you choose) and see if viewing the image through the PHP file causes the same error. If it does, then you know your script is at fault. – halfer Jul 18 '17 at 13:09
  • To understand what this script does, it uses PHP to load a JPEG and echo it to the user. It might need a suitable header as well (e.g. `header("Content-Type: image/jpeg");` at the start, but that's not too important - the point is whether or not you get a 502 error. – halfer Jul 18 '17 at 13:10
  • If this script works then you will need to debug your existing script. To do that, remove sections of it, until you find the cause of the 502. – halfer Jul 18 '17 at 13:11
  • I think I followed what you wanted me to do... 1. created a new image.php containing only – Bobby Gavin Jul 18 '17 at 15:32
  • Interestingly when I upload the original file again and run it via the address bar, I receive the following, which I wonder might shed any light... Warning: session_start() [function.session-start]: Cannot find save handler 'memcache' - session startup failed in /var/sites/c/charityfilm.co.uk/public_html/brandint/classes/image.php on line 1 – Bobby Gavin Jul 18 '17 at 15:37
  • and continuing from previous comment... Warning: Cannot modify header information - headers already sent by (output started at /var/sites/c/charityfilm.co.uk/public_html/brandint/classes/image.php:1) in /var/sites/c/charityfilm.co.uk/public_html/brandint/classes/image.php on line 1 Warning: Cannot modify header information - headers already sent by (output started at /var/sites/c/charityfilm.co.uk/public_html/brandint/classes/image.php:1) in /var/sites/c/charityfilm.co.uk/public_html/brandint/classes/image.php on line 11 – Bobby Gavin Jul 18 '17 at 15:38
0

Following on from the debugging you have done, here are some remarks.

Syntax errors

If there is a syntax error, then yes, it won't work. That seems to be fixed now, it was probably just uploaded wrongly.

Session configuration

Here is the new error you're getting:

Warning: session_start() [function.session-start]: Cannot find save handler 'memcache' - session startup failed in /var/sites/c/charityfilm.co.uk/public_html/brandint/classes/image.php on line 1

Warning: Cannot modify header information - headers already sent by (output started at /var/sites/c/charityfilm.co.uk/public_html/brandint/classes/image.php:1) in /var/sites/c/charityfilm.co.uk/public_html/brandint/classes/image.php on line 1

Warning: Cannot modify header information - headers already sent by (output started at /var/sites/c/charityfilm.co.uk/public_html/brandint/classes/image.php:1) in /var/sites/c/charityfilm.co.uk/public_html/brandint/classes/image.php on line 11

You can ignore the "Cannot modify header information" for now, since it is caused by the outputting of the first error. The first error indicates that you are operating a non-standard session system in PHP. Do you happen to know if there is a Memcache installation on the server?

This arrangement is fine if intentional, but PHP normally uses a file session system. I would expect this is being run as a result of a configuration in your PHP configuration file, usually called php.ini. Unusually, Go Daddy allows this file to be customised even on their shared host offering, which is why I think that might be the thing to check next.

There are three possible solutions:

  1. Modify the php.ini configuration so that it uses the standard session system, though that may well break something else on your website;
  2. Include a library used by your main app to set up the Memcache session system properly;
  3. Make a call just inside your image.php script to change the session system to file-based, for that feature only. This is not ideal, as you don't usually want various features on your website to use different session systems.

Since this is not possible to answer without exploring your server, it is becoming rather too broad for a Stack Overflow question. I suggest you go back to your original developer and ask him/her how to hook into the session system (option 2).

At a stretch, you could examine other PHP scripts in this website, to see what code they call that initialises the memcache session system. However, that's based on the assumption that this is not a misconfiguration, and that some existing code in your system makes use of sessions. If your old developer cannot help, I wonder if it is worth getting a freelancer in for an hour or two?

Community
  • 1
  • 1
halfer
  • 19,824
  • 17
  • 99
  • 186
  • I was suggested to upload the page and subfolders to another domain away from the charityfilm domain, and when I ran the page to same issue seemed to be repeating, however when I ran the image.php file in the browser under the new domain, it simply displayed... "502 Bad Gateway The server returned an invalid or incomplete response." with no additional errors or warnings. Does this have any significance? – Bobby Gavin Jul 18 '17 at 21:13
  • I noted the use of the term Memcache, which I never heard of until today where the host referred to it in an email... "I have tried to update the way that the sessions are saved to be in a file instead of memcache, but that brings up the 502 bad gateway error." - I have no idea what most of this means, nor it's significance. – Bobby Gavin Jul 18 '17 at 21:19
  • "Does this have any significance?" - difficult to say, you might just have on-screen errors turned off on that server. – halfer Jul 18 '17 at 21:20
  • "I have no idea what most of this means", yes, some of this is not trivial to wade through. I think you need to get someone in. – halfer Jul 18 '17 at 21:21
  • 1
    Due to deadline passing, I have reluctantly opted to remove the verification requirement from the contact.php as instructed. I thank you sincerely for all your assistance and patience on this matter halfer. – Bobby Gavin Jul 20 '17 at 13:54