I get session and cookie warning when I upload my php code to a server
I get this:
Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /www/zxq.net/w/e/e/weedcl/htdocs/index.php:3) in /www/zxq.net/w/e/e/weedcl/htdocs/common.php on line 2 Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /www/zxq.net/w/e/e/weedcl/htdocs/index.php:3) in /www/zxq.net/w/e/e/weedcl/htdocs/common.php on line 2 Warning: Cannot modify header information - headers already sent by (output started at /www/zxq.net/w/e/e/weedcl/htdocs/index.php:3) in /www/zxq.net/w/e/e/weedcl/htdocs/common.php on line 3 Warning: Cannot modify header information - headers already sent by (output started at /www/zxq.net/w/e/e/weedcl/htdocs/index.php:3) in /www/zxq.net/w/e/e/weedcl/htdocs/common.php on line 12
Its weird because everything works fine in localhost
common.php:
<?php
session_start();
header('Cache-control: private'); // IE 6 FIX
if(isSet($_GET['lang']))
{
$lang = $_GET['lang'];
// register the session and set the cookie
$_SESSION['lang'] = $lang;
setcookie("lang", $lang, time() + (3600 * 24 * 30));
}
else if(isSet($_SESSION['lang']))
{
index.php:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<?php
include_once 'common.php';
?>
<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.corner.js"></script>
<script type="text/javascript" src="scripts/jquery.validate.js"></script>
<script type="text/javascript" src="scripts/custom.js"></script>
<script type="text/javascript" src="scripts/slimbox2.js"></script>
</head>
<body id="home">
<div id="header">
<div class="container">
<div id="header-top">
<h1><a href="http://widerdesign.co.nr/">wider design</a></h1>
<ul id="lang">
<li><a href="index.php?lang=en" <?php if($lang=='en') {echo 'class="current"';} ?>>English</a></li>
<li><a href="index.php?lang=es" <?php if($lang=='es') {echo 'class="current"';} ?>>Español</a></li>
<li><a href="index.php?lang=tw" <?php if($lang=='tw') {echo 'class="current"';} ?>>中文(繁體)</a></li>
<li><a href="index.php?lang=cn" <?php if($lang=='cn') {echo 'class="current"';} ?>>中文(简体)</a></li>
</ul>
</div>
<div id="header-bottom">
<div id="tagline">
<h2>Nulla vitae tortor mauris. Fusce dapibus ultrices nibh id dignissim.<strong>Phasellus eget nibh ac quam rutrum mollis</strong> at vitae nisl.</h2>
<p>Pellentesque faucibus est eu tellus varius in suscipit augue dapibus. In turpis ligula, faucibus eu mollis non, tincidunt ac magna. Integer tempor laoreet lacus, non accumsan ligula eleifend a. Nulla vitae tortor mauris. Fusce dapibus ultrices nibh id dignissim. Phasellus eget nibh ac quam rutrum mollis at vitae nisl. Morbi ultricies tristique tortor sed elementum. Vivamus id neque et lectus commodo tempor.</p>
</div>
<div id="about">
<h2><?php echo $lang['h1']; ?></h2>
<p>Pellentesque fauc开发者_Python百科ibus est eu tellus varius in suscipit augue dapibus. In turpis ligula, faucibus eu mollis non, tincidunt ac magna. Integer tempor laoreet lacus, non accumsan ligula eleifend a. Nulla vitae tortor mauris. Fusce dapibus ultrices nibh id dignissim. Phasellus eget nibh ac quam rutrum mollis at vitae nisl. Morbi ultricies tristique tortor sed elementum. Vivamus id neque et lectus commodo tempor. Integer tincidunt, nunc ac hendrerit vestibulum, orci nisl commodo odio, a tempus leo libero nec sapien. Praesent a urna non diam mollis tristique.</p>
</div>
</div><!-- #header-bottom -->
</div><!-- .container -->
</div><!-- #header -->
<div id="work">
<div class="container">
<div class="project">
<div class="left">
<h2>Royal Steel</h2>
<p>Royal Steel Plastic Technology Industrial Co., Ltd., manufactures the dominant PET Plastic Recycling and Extrusion Equipment in the Industry. Since 1980, over 29 years experience in plastic recycling technology.</p>
<ul>
<li><a href="index.html">View site</a></a></li>
<li><a href="index.html">Visit site</a></li>
</ul>
</div>
<div class="right">
<a href="images/project1.jpg" rel="lightbox"><img src="images/project1.jpg"/></a>
</div>
</div><!-- .project -->
<div class="project">
<div class="left">
<h2>Best Language School</h2>
<p>Best Language school in Taiwan by provides personal service that exceeds expectations and by helping our students to achieve personal excellence.</p>
<ul>
<li><a href="index.html">View site</a></a></li>
<li><a href="index.html">Visit site</a></li>
</ul>
</div>
<div class="right">
<a href="#"><img src="images/project2.jpg"/></a>
</div>
</div><!-- .project -->
<div class="project">
<div class="left">
<h2>Best Language School</h2>
<p>Best Language school in Taiwan by provides personal service that exceeds expectations and by helping our students to achieve personal excellence.</p>
<ul>
<li><a href="index.html">View site</a></a></li>
<li><a href="index.html">Visit site</a></li>
</ul>
</div>
<div class="right">
<a href="#"><img src="images/project3.jpg"/></a>
</div>
</div><!-- .project -->
<div class="project">
<div class="left">
<h2>Best Language School</h2>
<p>Best Language school in Taiwan by provides personal service that exceeds expectations and by helping our students to achieve personal excellence.</p>
<ul>
<li><a href="index.html">View site</a></a></li>
<li><a href="index.html">Visit site</a></li>
</ul>
</div>
<div class="right">
<a href="#"><img src="images/project4.jpg"/></a>
</div>
</div><!-- .project -->
</div><!-- .container -->
</div><!-- #work -->
<div id="footer">
<div class="container">
<div id="footer-top">
<div id="contact">
<h2>Get in Touch</h2>
<p>Please use the following contact form to send me a message.</p>
<form id="sendmail" method="post" action="http://www.emailmeform.com/fid.php?formid=254816">
<label for="name"><strong>Your Name</strong></label>
<input type="text" id="name" name="FieldData0" />
<label for="email"><strong>Your Email</strong></label>
<input type="text" id="email" name="FieldData1" />
<label for="message"><strong>Your Message</strong></label>
<textarea type="text" id="message" name="FieldData2"></textarea>
<input id="button" type="submit" value="Send">
</form>
</div>
<div id="tools">
<h2>I use</h2>
<ul>
<li><a href="index.php">Vim</a></li>
<li><a href="index.php">Firefox</a></li>
<li><a href="index.php">Firebug</a></li>
<li><a href="index.php">jQuery</a></li>
<li><a href="index.php">Zymic</a></li>
<li><a href="index.php">Email Me Form</a></li>
</ul>
</div>
<div id="info">
<h2>Contact Details</h2>
<ul>
<li><strong>Address:</strong> Kuo Kuang Rd., Taichung 402, Taiwan R.O.C.</li>
<li><strong>Tel:</strong> 0918051170</li>
<li><strong>Email:</strong> <a href="mailto:janoochen@gmail.com">janoochen@gmail.com</a></li>
</ul>
</div>
</div><!-- #footer-top -->
<div id="footer-bottom">
<p>Copyright © 2009 New Project. All Rights Reserved.</p>
</div>
</div><!-- .container -->
</div><!-- #footer -->
</body>
</html>
The error says it all:
headers already sent (output started at ..../index.php:3)
You can't send headers after the body output has started. It works on your home machine because either warnings are suppressed, or you have different output buffer settings.
Use
<?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">
The setcookie()
will write to the response headers, which is only possible if the response isn't already been committed. A response will be committed when you write something to the response body and the server sends it to the client. In this case, the doctype is been written to the response before the setcookie()
is called. You need to move the include to the top of the page:
<?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">
As to why it works in localhost, it may be dependent on the server config whether the response will be directly committed or buffered in memory. In localhost it's apparently been buffered so that it leaves room to change the response headers after even writing a small piece of response body.
Your file is probably stored as UTF8 file that contents few characters before your php start tag
Save it as plain ANSII file without Unicode format and re-try. Be careful not to lose any Unicode characters within your scripts if any.
精彩评论