Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added get_header method to Output class. #645

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 71 additions & 8 deletions system/core/Output.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*
Expand Down Expand Up @@ -172,6 +172,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])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

|| 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
*
Expand Down Expand Up @@ -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 */
3 changes: 2 additions & 1 deletion user_guide_src/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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>`.
Expand Down Expand Up @@ -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
------------------
Expand Down
8 changes: 8 additions & 0 deletions user_guide_src/source/libraries/output.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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();
=============================

Expand Down