Skip to content

Commit

Permalink
Prevents getting HTTP header related error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabrice Luraine committed Jun 5, 2009
1 parent bc6df0f commit a0a8bf1
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions lib/limonade.php
Expand Up @@ -1189,7 +1189,7 @@ function render($content_or_func, $layout = '', $locals = array())
*/
function html($content_or_func, $layout = '', $locals = array())
{
header('Content-Type: text/html; charset='.strtolower(option('encoding')));
if(!headers_sent()) header('Content-Type: text/html; charset='.strtolower(option('encoding')));
$args = func_get_args();
return call_user_func_array('render', $args);
}
Expand Down Expand Up @@ -1217,7 +1217,7 @@ function layout($function_or_file = null)
*/
function xml($data)
{
header('Content-Type: text/xml; charset='.strtolower(option('encoding')));
if(!headers_sent()) header('Content-Type: text/xml; charset='.strtolower(option('encoding')));
$args = func_get_args();
return call_user_func_array('render', $args);
}
Expand All @@ -1232,7 +1232,7 @@ function xml($data)
*/
function css($content_or_func, $layout = '', $locals = array())
{
header('Content-Type: text/css; charset='.strtolower(option('encoding')));
if(!headers_sent()) header('Content-Type: text/css; charset='.strtolower(option('encoding')));
$args = func_get_args();
return call_user_func_array('render', $args);
}
Expand All @@ -1247,7 +1247,7 @@ function css($content_or_func, $layout = '', $locals = array())
*/
function txt($content_or_func, $layout = '', $locals = array())
{
header('Content-Type: text/plain; charset='.strtolower(option('encoding')));
if(!headers_sent()) header('Content-Type: text/plain; charset='.strtolower(option('encoding')));
$args = func_get_args();
return call_user_func_array('render', $args);
}
Expand All @@ -1261,7 +1261,7 @@ function txt($content_or_func, $layout = '', $locals = array())
*/
function json($data, $json_option = 0)
{
header('Content-Type: application/x-javascript; charset='.strtolower(option('encoding')));
if(!headers_sent()) header('Content-Type: application/x-javascript; charset='.strtolower(option('encoding')));
return version_compare(PHP_VERSION, '5.3.0', '>=') ? json_encode($data, $json_option) : json_encode($data);
}

Expand Down Expand Up @@ -1290,7 +1290,7 @@ function render_file($filename, $return = false)
$content_type = mime_type(file_extension($filename));
$header = 'Content-type: '.$content_type;
if(file_is_text($filename)) $header .= 'charset='.strtolower(option('encoding'));
header($header);
if(!headers_sent()) header($header);
return file_read($filename, $return);
}
else halt(NOT_FOUND, "unknown filename $filename");
Expand Down Expand Up @@ -1547,8 +1547,11 @@ function array_to_xml($data, $rootNodeName = 'data', &$xml=null)
*/
function status($code = 500)
{
$str = http_response_status_code($code);
header($str);
if(!headers_sent())
{
$str = http_response_status_code($code);
header($str);
}
}

/**
Expand All @@ -1566,8 +1569,11 @@ function redirect($uri)
# one yourself.

# TODO make absolute uri
header('Location: '.$uri);
exit;
if(!headers_sent())
{
header('Location: '.$uri);
exit;
}
}

/**
Expand Down

0 comments on commit a0a8bf1

Please sign in to comment.