This question already has an answer here:
I'm getting this:
Warning: Cannot modify header information - headers already sent by
(output started at /www/zxq.net/a/l/e/alexchen/htdocs/index.php:12) in /www/zxq.net/a/l/e/alexchen/htdocs/common.php on line 13
right on my h2
tag.
I have my common.php
before the html code. I don't know what the problem is. Help!
index.php:
<?php
include_once 'common.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"/>
<title>New Project</title>
<link rel="stylesheet" type="text/css" href="styles/global.css" />
<link rel="stylesheet" type="text/css" href="styles/slimbox2.css" />
<script type="text/javascript" src="scripts/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="scripts/jquery.validate.js"></script>
<script type="text/javascript" src="scripts/slimbox2.js"></script>
<script type="text/javascript" src="scripts/jquery.scrollTo-min.js"></script>
<script type="text/javascript" src="scripts/jquery.localscroll-min.js"></script>
<script type="text/javascript" src="scripts/custom.js"></script>
<?php if(get_lang()=='en') {echo '<script type="text/javascript" src="scripts/jquery-validate/val-en.js"></script>';} ?>
<?php if(get_lang()=='es') {echo '<script type="text/javascript" src="scripts/jquery-validate/val-es.js"></script>';} ?>
<?php if(get_lang()=='tw') {echo '<script type="text/javascript" src="scripts/jquery-validate/val-tw.js"></script>';} ?>
<?php if(get_lang()=='cn') {echo '<script type="text/javascript" src="scripts/jquery-validate/val-cn.js"></script>';} ?>
common.php:
<?php
session_start();
header('Cache-control: private'); // IE 6 FIX
function get_lang(){
if(!empty($_GET['lang'])) return $_GET['lang'];
if(!empty($_SESSION['lang'])) return $_SESSION['lang'];
if(!empty($_COOKIE['lang'])) return $_COOKIE['lang'];
return 'en';
}
function set_lang($lang){
setcookie("lang", $lang, time() + (3600 * 24 * 30));
$_SESSION['lang'] = $lang;
}
function get_lang_file($lang){
$lang_file = "languages/lang.$lang.php";
if(file_exists($lang_file)) return $lang_file;
if($lang_file = get_lang_file('en')) return $lang_file;
return false;
}
//translation helper function
function l($string){
static $translation;
if(!isset($translation)){
$lang = get_lang();
$lang_file = get_lang_file($lang);
if($lang_file) set_lang($lang);
$translation = include $lang_file;
}
return $translation[$string];
}
?>
It can happen for several reasons:
You are sending HTTP headers indirecly, via setcookie() or session_start().
Have a look at your set_lang() function. You can fix it by calling l() before output.
You have whitespace before the PHP open tag or after PHP close tag.
You saved the file in UTF-8, but with BOM. Save it without BOM.
You can always add ob_start(), but this will only bypass the problem.
Setting a cookie requires sending a header, so you cannot call
set_lang()
once you have output any of the page unless you use the output buffering functions.Try putting
ob_start()
function at the top of file where you are getting this error.Your
l()
function is callingset_lang()
the first time it is used, which sets a cookie -- and cookies are sent as HTTP-headers.I'm guessing, from the look of it, that
l()
is used to get the translated version of a string -- which means it's probably used from everywhere in your HTML/PHP ; i.e. after the output has begun being sent.You should call
set_lang()
at the top of yourcommon.php
file -- to make sure the cookie is sent before any HTML content.In fact, in your case, I would modify the
l()
function so it only does one thing : return a translated string.I would put the initialization of your translation system out of
l()
-- which means initializing it "by hand" at the beginning of your script, yes ; but also means a simplerl()
function that will have less work to do.