I have a php script which generates an image, and is used (mainly) like this:
<img src="user_image.php?id=[some_guid]" />
The script uses a class I wrote to display an image matching that ID. There are a number of things that could go wrong though, and each of them throws an exception. So I have something like this:
<?php
try {
if( ! isset($_GET['id']) ) throw new Exception;
$images = new User_Images;
$images->display($_GET['id']);
} catch( Exception $e ) {
header('location: images/link_error.png');
}
If I view this from the browser, everything is fine -- if there was an error the address in the address bar changes to images/link_error.png
and displays that instead.
But when this script is used in an <img>
tag, and there is an error grabbing the image, it doesn't show up at all.
Do header redirects not work this way? What is another way that I can do this?
update
There is no problem, browser redirects work perfectly this way, the issue was that my browser was caching the empty image that was returned before the redirect was put in. A hard refresh (Ctrl + F5 for Firefox) fixed it and it started working like normal.