Added get_header method to Output class. #645

Closed
wants to merge 5 commits into
from
View
@@ -44,49 +44,49 @@ class CI_Output {
* @var string
*/
public $final_output;
-
+
/**
* Cache expiration time
*
* @var int
*/
public $cache_expiration = 0;
-
+
/**
* List of server headers
*
* @var array
*/
public $headers = array();
-
+
/**
* List of mime types
*
* @var array
*/
public $mime_types = array();
-
+
/**
* Determines wether profiler is enabled
*
* @var book
*/
public $enable_profiler = FALSE;
-
+
/**
* Determines if output compression is enabled
*
* @var bool
*/
protected $_zlib_oc = FALSE;
-
+
/**
* List of profiler sections
*
* @var array
*/
protected $_profiler_sections = array();
-
+
/**
* Whether or not to parse variables like {elapsed_time} and {memory_usage}
*
@@ -173,6 +173,69 @@ public function append_output($output)
// --------------------------------------------------------------------
/**
+ * Get Header
+ *
+ * Retrieve an individual header or an array of headers that have
+ * already been sent via Output class or header() function.
+ *
+ * For an individual header, the value is returned. For an array
+ * of headers, a key-value array is returned.
+ *
+ * @param string
+ * @return array
+ */
+ public function get_header($header = NULL)
+ {
+ $headers = array();
+
+ // Get headers that have already been sent
+ $sent_headers = headers_list();
+ if ( ! empty($sent_headers))
+ {
+ foreach ($sent_headers as $sent_header)
+ {
+ $header_array = explode(':', $sent_header);
+ $field = trim($header_array[0]);
+ $value = trim($header_array[1]);
+ $headers[$field] = $value;
+ }
+ }
+
+ // Get headers set via the Output class
+ $output_headers = $this->headers;
+ if ( ! empty($output_headers))
+ {
+ foreach ($output_headers as $output_header)
+ {
+ $header_array = explode(':', $output_header[0]);
+ $field = trim($header_array[0]);
+ $value = trim($header_array[1]);
+
+ // Add new or replace existing when specified
+ if ( ! isset($headers[$field]) || $output_header[1])
@narfbg

narfbg Jun 15, 2012

Contributor

|| must be changed to OR, as per the style guide.

+ {
+ $headers[$field] = $value;
+ }
+ }
+ }
+
+ if ( ! empty($headers) && empty($header))
+ {
+ return $headers;
+ }
+ elseif (isset($headers[$header]))
+ {
+ return $headers[$header];
+ }
+ else
+ {
+ return FALSE;
+ }
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
* Set Header
*
* Lets you set a server header which will be outputted with the final display.
@@ -548,4 +611,4 @@ public function _display_cache(&$CFG, &$URI)
}
/* End of file Output.php */
-/* Location: ./system/core/Output.php */
+/* Location: ./system/core/Output.php */
@@ -55,7 +55,7 @@ Release Date: Not Released
- Database
- - Renamed the Active Record class to Query Builder to remove confusion with
+ - Renamed the Active Record class to Query Builder to remove confusion with
the Active Record design pattern
- Added new :doc:`Query Builder <database/query_builder>` methods that return
- Added the ability to insert objects with insert_batch() in :doc:`Query Builder <database/query_builder>`.
@@ -146,6 +146,7 @@ Release Date: Not Released
- Added support for HTTP-Only cookies with new config option ``cookie_httponly`` (default FALSE).
- Renamed method _call_hook() to call_hook() in the :doc:`Hooks Library <general/hooks>`.
- Added get_content_type() method to the :doc:`Output Library <libraries/output>`.
+ - Added get_header() method to the :doc:`Output Library <libraries/output>`.
Bug fixes for 3.0
------------------
@@ -77,6 +77,14 @@ Appends data onto the output string. Usage example::
$this->output->append_output($data);
+$this->output->get_header();
+=============================
+
+Retrieves headers that have been set by the Output class, or by using
+the traditional header() function. Example::
+
+ $this->output->get_header('Content-Type');
+
$this->output->set_header();
=============================