tags:

views:

64

answers:

4

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"&gt;
<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:[email protected]">[email protected]</a></li>
                    <li id="skype"><p></p>alexchen.net</li>
                    <li id="facebook"><a href="http://www.facebook.com/weedd.chen"&gt;facebook&lt;/a&gt;&lt;/li&gt;
                </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"&gt;
                <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"&gt;Zymic&lt;/a&gt;. Contact form hosted on <a href="http://www.emailmeform.com"&gt;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>

live:

http://alexchen.co.nr/test

A: 

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..).

kgb
+2  A: 

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.

Gumbo
@Gumbo I'm confused, is the problem in line 10 or line 14 (localization.php)?
janoChen
@janoChen: The error occurs in *localization.php* but the cause of this error is in *index.php*. So if you fix the issue of premature output in *index.php* the HTTP header modifying function call in *localization.php* will succeed.
Gumbo
@Gumbo sorry I'm a bit lost, does it have something to do with the position of <?phpinclude_once 'localization.php';?> (line number 1, 2 and 3 of index.php) or "(get_lang() ==" in line 17?
janoChen
@janoChen: No. Just look at the positions given in the error message.
Gumbo
+1  A: 

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.

George Marian
@George Marian thanks for the response. Is there any way of fixing this? Do I have to move the set_lang function?
janoChen
Basically, l() needs to be modified, so that it doesn't call set_lang(). set_lang() would then need to be called somewhere else. Based on your other question, there's a difference between the function l() in the two files. In the original one, l() didn't set a cookie. That was done at the top of the file. So, what's happened is that l() has changed it's behavior between the two files. It's behavior in this version of localization.php is inappropriate for the way l() is being used.
George Marian
@George Marian, Yes I was thinking to just use the working version of my previous post: http://stackoverflow.com/questions/3174353/two-simple-php-files-for-localization-that-do-basically-the-same-but-one-doesnt
janoChen
@janoChen I was going to ask you why you were trying to use a different file or make changes. Other than pretty functions, I don't see much of a difference. Since you're not a PHP programmer, "improving" the code to use as a sample for some job application -- or whatever -- isn't the case for you.
George Marian
@George Marian I know, but I thought that by simplifying a php file I could improve the quality of my website and learn PHP in the process. But If think you are right, maybe is not so much improvement between the working file and the one that doesn't work, I'm not very sure.
janoChen
@janoChen Improvements can, indeed, be made to the original version of the file. However, the improvements aren't necessarily functionality improvements. They would basically be improvements to the syntax, which would make it easier to make future modifications to the functionality. In other words, I suggest you play around with PHP, follow some tutorials, do some research/readhing and then come back try these modifications. There's a bunch of basic stuff you need to get under your belt before you should attempt to refactor code. Good luck.
George Marian
+1  A: 

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

nathan
@nathan Thanks for providing the code, but how should I call the CSS file with php in line 17?: <?php if(get_lang() == 'zh-tw' || get_lang() == 'zh-cn') {echo '<link rel="stylesheet" type="text/css" href="styles/chinese.css" />';} ?> I also tried: <?php if(get_lang() == 'zh-tw' || get_lang() == 'zh-cn') {echo '<link rel="stylesheet" type="text/css" href="styles/chinese.css" />';} ?>but in both cases it tells me that I'm calling an undefined function.
janoChen
use if($_SESSION['lang'] == 'zh-tw' || $_SESSION['lang'] == 'zh-cn') instead :)
nathan