note 88775 deleted from function.header by tularis
| From: | tularis@php.net | Date: | Fri, 04 Apr 2014 11:26:43 +0000 |
| Subject: | note 88775 deleted from function.header by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-199151@lists.php.net to get a copy of this message | ||
Note Submitter: nullhility at gmail dot com
----
I ran into problems when trying to get php to use a transparent ErrorDocument redirect.
My document root .htaccess was set up to redirect select error codes to error.php which tests
against $_SERVER['REDIRECT_STATUS'] and templates a document based on the status code.
I had hoped to raise a 404 after a mysql query turned up empty (fyi; before output had started) but
page output continued "normally", ie incorrectly.
Because this was inside a template class I was able to manually change the template's include
file and $_SERVER['REDIRECT_STATUS'] to output the desired result. I wasn't actually
sure if header() worked or not because apache2 has already returned 200 OK after which any header
change would only affect the browser, wouldn't it?
I have read about header() working with apache redirects before, is this still available with
apache2? Here's some code just to explain the concept:
<?php
class page_template
{
$include_file;
//...
function getData ()
{
try {
//...
if ($result->num_rows == 0) {
throw new MysqlException("Empty result set");
}
//...
} catch (MysqlException $e) {
if ($e->getMessage() == "Empty result set") {
header("HTTP/1.0 404 Not Found");
header("Status: 404 Not Found");
//the below two lines fixed the issue for me
$_SERVER['REDIRECT_STATUS'] = 404;
$this->template_file =
$_SERVER['DOCUMTENT_ROOT'].'error.php';
}
}
}
?>