note 88775 added to function.header

From: Date: Sat, 07 Feb 2009 00:15:02 +0000
Subject: note 88775 added to function.header
Groups: php.notes 
Request: Send a blank email to php-notes+get-150328@lists.php.net to get a copy of this message
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'; } } } ?> ---- Server IP: 117.104.160.194 Probable Submitter: 124.177.95.8 ---- Manual Page -- http://www.php.net/manual/en/function.header.php Edit -- https://master.php.net/note/edit/88775 Del: integrated -- https://master.php.net/note/delete/88775/integrated Del: useless -- https://master.php.net/note/delete/88775/useless Del: bad code -- https://master.php.net/note/delete/88775/bad+code Del: spam -- https://master.php.net/note/delete/88775/spam Del: non-english -- https://master.php.net/note/delete/88775/non-english Del: in docs -- https://master.php.net/note/delete/88775/in+docs Del: other reasons-- https://master.php.net/note/delete/88775 Reject -- https://master.php.net/note/reject/88775 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#150328) next »