PDA

View Full Version : Custom error page



Justin
02-07-2009, 08:54 AM
Hi everyone,

I'm using the following code for my custom error 404 page. It redirects fine but the loading image doesn't show.

<?php
ob_start();
echo "Redirecting......";
echo '<img src="http://www.mysite.com/images/redirect_inAction.gif"; alt="You\'re being redirected" width="186" height="42" />';
echo '<meta http-equiv="refresh" content="8;url=http://www.mysite.com/" />';
ob_flush();
?>I see '; echo ''; ob_flush(); ?> where I placed the PHP code, when I preview it. Can anyone help me out? Any help will be much appreciated.

wysiwyg
02-07-2009, 10:36 AM
You're basically just echoing html here, so why even use a php script?

It might as well be this..


<meta http-equiv="refresh" content="8;url=http://www.mysite.com/" />
Redirecting......<img src="http://www.mysite.com/images/redirect_inAction.gif" alt="You're being redirected" width="186" height="42" />


You have a semi-colon in your image tag, but even that shouldn't prevent it from displaying with the code you have. It sounds like the src doesn't have a proper url.

If you do want to use php for a delayed redirect, it can be done with a refresh header - apparently a little known secret, because I frequently see people using a meta tag to do this.



<?php
ob_start(); // buffers output, optional
header('refresh: 8; url=http://www.mysite.com'); // redirect after 8 seconds
echo 'Redirecting......<img src="http://www.mysite.com/images/redirect_inAction.gif" alt="You\'re being redirected" width="186" height="42" />';
ob_flush(); // outputs buffer to page
?>


edit: Also, whatever you're using to preview your page is not processing it as php, it's just outputting html. Anything between < and > will be treated as a tag, and will be hidden, which is why you're seeing what you are.

Actually, this just occurred to me, what extension are you using for this error page?

felgall
02-07-2009, 01:01 PM
If you do want to use php for a delayed redirect, it can be done with a refresh header - apparently a little known secret, because I frequently see people using a meta tag to do this.
[/php]

All the meta http-equiv are just copies into the HTML of headers that are better done by setting the header properly beforehand as can be done with PHP. The only difference is that if you use a meta tag to do it in the HTML it will not always work because it is then subject to browser settings whereas if you set it properly in the page headers before the start of the HTML it will work more reliably.

There should never be a need to use meta http-equiv if the page uses server side scripting.

Justin
02-08-2009, 01:14 AM
Thanks both of of you, wysiwyg and felgall, for your kind response.

wysiwyg, I tried the code you gave to me and it does not work either. To answer your question, I saved the file as 404.shtml (the format Bluehost allows).

The strange thing is that I'm using exactly the same code in my "thank you pages" (the pages that show after submitting web forms) and it works great.

wysiwyg
02-08-2009, 07:51 AM
SHTML isn't going to process PHP, it's for SSI.

While shtml is the default extension for error documents, you can set your own filenames for each response code via htaccess like this:


ErrorDocument 403 /forbidden.html
ErrorDocument 404 /404.php


My first example should work in the shtml page though, did you try that?

Justin
02-09-2009, 07:52 AM
wysiwyg,

I went back to try you first example again and it worked fine. The reason why it didn't work before was I enclosed the code in <?php ?>. My mistake! Thank you so much.