简体   繁体   中英

Failed to load resource: status 502 (Bad Gateway) image.php

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

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");

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM