Warning: Cannot modify header information (something wrong with a localization PHP file) [duplicate]
My web server shows me the following warning, and I think know where is the problem:
WARNING: CANNOT MODIFY HEADER INFORMATION - HEADERS ALREADY SENT BY (OUTPUT STARTED AT /WWW/ZXQ.NET/A/L/E/ALEXCHEN/HTDOCS/TEST/INDEX.PHP:10) IN /WWW/ZXQ.NET/A/L/E/ALEXCHEN/HTDOCS/TEST/LOCALIZATION.PHP ON LINE 14
But I don't know exactly how to solve it.
Any suggestions?
localization:
<?php
//set session and cookies
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)); //line 14
$_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;
}
//set translation helper function
function l($string){
static $localization;
if(!isset($localization)){
$lang = get_lang();
$lang_file = get_lang_file($lang);
if($lang_file) set_lang($lang);
$localization = include $lang_file;
}
return $localization[$string];
}
?>
index.php:
<?php
include_once 'localization.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta name="robots" content="index, follow"/>
<meta name="description" content="Web design and Translation / 網頁設計和翻譯" />
<meta name="keywords" content="web development, web developer, web design, web designer, translation, translator, taiwan, taipei, taichung, english, chinese, spanish, 網站開發者, 網頁設計, 網頁設計師, 翻譯, 翻譯著, 台灣, 台北, 台中, 英文, 中文, 西班牙文, html, css, javascript, php" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"/>
<title>Alex Chen - Web design and Translation / 網頁設計和翻譯</title>
<link rel="stylesheet" type="text/css" href="styles/reset.css" />
<link rel="stylesheet" type="text/css" href="styles/global.css" />
开发者_如何学运维 <link rel="stylesheet" type="text/css" href="fancybox/jquery.fancybox-1.3.1.css" />
<?php if(get_lang() == 'zh-tw' || get_lang() == 'zh-cn') {echo '<link rel="stylesheet" type="text/css" href="styles/chinese.css" />';} ?>
</head>
<body id="home">
<div id="header">
<div class="container">
<ul id="lang">
<li <?php if(get_lang() == 'en') {echo 'class="current"';} ?>><a href="index.php?lang=en">ENGLISH</a></li>
<li <?php if(get_lang() == 'es') {echo 'class="current"';} ?>><a href="index.php?lang=es">ESPAÑOL</a></li>
<li <?php if(get_lang() == 'zh-tw') {echo 'class="current"';} ?>><a href="index.php?lang=zh-tw">中文(繁體)</a></li>
<li <?php if(get_lang() == 'zh-cn') {echo 'class="current"';} ?>><a href="index.php?lang=zh-cn">中文(简体)</a></li>
</ul>
<div id="logo-bg">
<h1><a>ALEXCHEN</a></h1>
</div>
<div id="nav-bg">
<ul id="nav">
<li><a href="#content"><?php echo l('nav1'); ?></a></li>
<li><a href="#contact"><?php echo l('nav2'); ?></a></li>
</ul>
</div>
</div><!-- .container -->
</div><!-- #header -->
<div id="content">
<div class="container">
<div class="showcase">
<div class="first">
<div id="nopic">
<p><?php echo l('tagline_p'); ?></p>
</div>
</div><!-- .first -->
<div class="pusher">
<h3><?php echo l('showcase1_h3'); ?></h3>
<p><?php echo l('showcase1_p'); ?></p>
<div class="pic">
<a id="showcase1" href="showcase/showcase1.php"><img src="images/showcase1t.png"/></a>
</div><!-- .pic -->
</div><!-- .pusher -->
<div class="pusher">
<h3><?php echo l('showcase2_h3'); ?></h3>
<p><?php echo l('showcase2_p'); ?></p>
<div class="pic">
<a id="showcase2" href="showcase/showcase2.php"><img src="images/showcase2t.png"/></a>
</div><!-- .pic -->
</div><!-- .pusher -->
<div class="pusher">
<h3><?php echo l('showcase4_h3'); ?></h3>
<p><?php echo l('showcase4_p'); ?></p>
<div class="pic">
<a id="showcase4" href="showcase/showcase4.php"><img src="images/showcase4t.png"/></a>
</div><!-- .pic -->
</div><!-- .pusher -->
<div class="pusher">
<h3><?php echo l('showcase9_h3'); ?></h3>
<p><?php echo l('showcase9_p'); ?></p>
<div class="pic">
<a id="showcase9" href="showcase/showcase9.php"><img src="images/showcase9t.png"/></a>
</div><!-- .pic -->
</div><!-- .pushed -->
<div class="pusher">
<h3><?php echo l('showcase5_h3'); ?></h3>
<p><?php echo l('showcase5_p'); ?></p>
<div class="pic">
<a id="showcase5" href="showcase/showcase5.php"><img src="images/showcase5t.png"/></a>
</div><!-- .pic -->
</div><!-- .pusher -->
<div class="pusher">
<h3><?php echo l('showcase8_h3'); ?></h3>
<p><?php echo l('showcase8_p'); ?></p>
<div class="pic">
<a id="showcase8" href="showcase/showcase8.php"><img src="images/showcase8t.png"/></a>
</div><!-- .pic -->
</div><!-- .pushed -->
<div class="pusher">
<h3><?php echo l('showcase6_h3'); ?></h3>
<p><?php echo l('showcase6_p'); ?></p>
<div class="pic">
<a id="showcase6" href="showcase/showcase6.php"><img src="images/showcase6t.png"/></a>
</div><!-- .pic -->
</div><!-- .pusher -->
<div class="pusher">
<h3><?php echo l('showcase7_h3'); ?></h3>
<p><?php echo l('showcase7_p'); ?></p>
<div class="pic">
<a id="showcase7" href="showcase/showcase7.php"><img src="images/showcase7t.png"/></a>
</div><!-- .pic -->
</div><!-- .pushed -->
</div><!-- .showcase -->
<div class="pusher">
<h3><?php echo l('showcase3_h3'); ?></h3>
<p><?php echo l('showcase3_p'); ?></p>
<div class="pic">
<a id="showcase3" href="showcase/showcase3.php"><img src="images/showcase3t.png"/></a>
</div><!-- .pic -->
</div><!-- .pusher -->
</div><!-- .container -->
</div><!-- #work -->
<div id="footer">
<div class="container">
<div id="footer-top">
<div id="about">
<h3><?php echo l('about_h3'); ?></h3>
<img src="images/profile.png"/>
<p><?php echo l('about_p'); ?></p>
</div>
<div id="info">
<h3><?php echo l('info_h3'); ?></h3>
<ul>
<li id="mobile"><p></p>0918051170</li>
<li id="gmail"><p></p><a href="mailto:janoochen@gmail.com">alexchen.net@gmail.com</a></li>
<li id="skype"><p></p>alexchen.net</li>
<li id="facebook"><a href="http://www.facebook.com/weedd.chen">facebook</a></li>
</ul>
</div>
<div id="contact">
<h3><?php echo l('contact_h3'); ?></h3>
<p><?php echo l('contact_p'); ?></p>
<form id="sendmail" method="post" action="http://www.emailmeform.com/fid.php?formid=254816">
<label for="name"><?php echo l('form_label1'); ?></label>
<input type="text" id="name" name="FieldData0" />
<label for="email"><?php echo l('form_label2'); ?></label>
<input type="text" id="email" name="FieldData1" />
<label for="message"><?php echo l('form_label3'); ?></label>
<textarea type="text" id="message" name="FieldData2"></textarea>
<input id="button" type="submit" value="<?php echo l('submit'); ?>">
</form>
</div>
</div><!-- #footer-top -->
<div id="footer-bottom">
<div id="rights">
<p>Copyright © 2010 Alex Chen. All Rights Reserved. Hosted on <a href="http://www.zymic.com">Zymic</a>. Contact form hosted on <a href="http://www.emailmeform.com">Email Me Form</a>.</p>
</div>
<div id="ads">
<a id="nr" href="http://www.freedomain.co.nr/cheap_web_hosting.php" target="_blank"><img src="http://ruarmza.4u.com.ru/ch.gif" alt="Cheap Web Hosting Providers" width="88" height="31" border="0" /></a>
</div>
</div><!-- #footer-bottom -->
</div><!-- .container -->
</div><!-- #footer -->
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-13164175-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script><!-- google analytics -->
<script type="text/javascript" src="scripts/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="scripts/jquery.validate.min.js"></script>
<script type="text/javascript" src="fancybox/jquery.fancybox-1.3.1.pack.js"></script>
<script type="text/javascript" src="scripts/jquery.scroll.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() =='zh-tw') {echo '<script type="text/javascript" src="scripts/jquery-validate/val-zh-tw.js"></script>';} ?>
<?php if(get_lang() =='zh-cn') {echo '<script type="text/javascript" src="scripts/jquery-validate/val-zh-cn.js"></script>';} ?>
</body>
</html>
The cause of this error is that modifying the HTTP header requires that the HTTP header is not already sent to the client. And this happens when the first data is put out (either implicitly or explicitly). So with the first data that is put out, the HTTP header is sent along to the client and cannot be modified afterwards.
In your case, according to the error message, some function call tries to modify the HTTP header at /TEST/LOCALIZATION.PHP on line 10 but the output has already started at /TEST/INDEX.PHP on line 1.
To fix this you either need to make sure that the modification of the HTTP header happens before the first output is done (input-process-output model) or you buffer any output with PHP’s output control functions.
The problem seems to be that the function l() calls set_lang(), after content is already sent to the browser. Meanwhile, set_lang() is trying to set a cookie, which must be done in the header, before any content is sent to the browser.
swap your localization.php for this one..
<?php
session_start();
header('Cache-control: private'); // IE 6 FIX
get_localization();
function get_localization()
{
static $localization;
if( empty($localization) ) {
$lang = 'en';
if(!empty($_GET['lang'])) $lang = $_GET['lang'];
if(!empty($_SESSION['lang'])) $lang = $_SESSION['lang'];
if(!empty($_COOKIE['lang'])) $lang = $_COOKIE['lang'];
setcookie("lang", $lang, time() + (3600 * 24 * 30)); //line 14
$_SESSION['lang'] = $lang;
$lang_file = "languages/lang.$lang.php";
if( !file_exists($lang_file) ) {
if( !$lang_file = get_lang_file('en') ) {
return false;
}
}
include $lang_file;
$localization = $lang;
}
return $localization;
}
function l($string){
$l = get_localization();
return is_array($l) && isset($l[$string]) ? $l[$string]: $string;
}
?>
should sort it
make sure that there is no output before calling header(). also, check if your files have BOM(which comes before <?php
and thus is output..).
精彩评论