473,387 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Strange error while trying to set a cookie


Hi,

Whenever I run a script page, I get this error:
Warning: Cannot modify header information - headers already sent by (output
started at c:\proyectos\websites\test\index.php:9) in c:\proyectos\websites\test\index.php
on line 11

My code is:

<?
$cookieValue = 'abra cadabra, pedo de cabra';
setcookie('aTestCookie', $cookieValue, time() + 3600);
?>

Any ideas? O:-)

Thanks
Jan 16 '06 #1
6 1486

Fernando Rodríguez wrote:
Hi,

Whenever I run a script page, I get this error:
Warning: Cannot modify header information - headers already sent by (output
started at c:\proyectos\websites\test\index.php:9) in c:\proyectos\websites\test\index.php
on line 11

My code is:

<?
$cookieValue = 'abra cadabra, pedo de cabra';
setcookie('aTestCookie', $cookieValue, time() + 3600);
?>

Any ideas? O:-)

Thanks


You get that error message because the headers have already been sent
and output has started. Specifically, you sent output on line 9 of
index.php. If you want to set HTTP headers (and setting a cookie
requires sending a header) you need to do it *before* you send any
other output to the browser -- even a single whitespace counts.

So, you have two options here:

1. Re-order the script so that you call setcookie() before you send any
output.
2. Use ob_start() and ob_flush() to buffer the output (see
<http://www.php.net/manual/en/ref.outcontrol.php> for more information).

Jan 16 '06 #2
Fernando Rodríguez wrote:
Whenever I run a script page, I get this error:
Warning: Cannot modify header information - headers already sent by (output
started at c:\proyectos\websites\test\index.php:9) in c:\proyectos\websites\test\index.php ________________________________________________^_ _____________ ... on line 11 ________^^
My code is:
1 <?
2 $cookieValue = 'abra cadabra, pedo de cabra';
3 setcookie('aTestCookie', $cookieValue, time() + 3600);
4 ?>
Any ideas? O:-)


Please post the rest of your code

I guess line 3 in the example you posted is actually line 11 in your
*real* script, making line 1 in the example your *real* line 9.

The script you posted had absolutely no reason (*) to issue that
warning.

(*) save auto_prepend_file

--
If you're posting through Google read <http://cfaj.freeshell.org/google>
Jan 16 '06 #3
*** Pedro Graca escribió/wrote (16 Jan 2006 21:17:11 GMT):
The script you posted had absolutely no reason (*) to issue that
warning.

(*) save auto_prepend_file


Or blank spaces before the first delimiter.
--
-+ Álvaro G. Vicario - Burgos, Spain
++ http://bits.demogracia.com es mi sitio para programadores web
+- http://www.demogracia.com es mi web de humor libre de cloro
--
Jan 16 '06 #4
Hello Pedro,
Please post the rest of your code

OK, here's the full code:

-----------------------------------
<html>
<head>
<title>Untitled Document</title>
<?
$cookieValue = 'abra cadabra, pedo de cabra';
setcookie('aTestCookie', $cookieValue, time() + 3600);
?>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

<ol>
<li> <a href="http://es2.php.net/FAQ.php">PHP FAQ</a></li>
<li> <a href="http://es.php.net/manual/en/tutorial.firstpage.php">PHP Tutorial</a></li>
<li> <a href="http://es.php.net/manual/en/index.php">PHP Manual</a></li>
</ol>
<form action="action.php" method="post">
<p>Your name: <input type="text" name="name" /></p>
<p>Your age: <input type="text" name="age" /></p>
<p><input type="submit" /></p>
</form>

</body>
</html>
------------------------------------

This causes the follwoing output:

Warning: Cannot modify header information - headers already sent by (output
started at c:\proyectos\websites\test\index.php:4) in c:\proyectos\websites\test\index.php
on line 6
Jan 17 '06 #5
Fernando Rodríguez wrote:
Hello Pedro,
Please post the rest of your code


OK, here's the full code:

-----------------------------------
<html>
<head>
<title>Untitled Document</title>
<? $cookieValue = 'abra cadabra, pedo de cabra';
setcookie('aTestCookie', $cookieValue, time() + 3600); ?>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

<ol>
<li> <a href="http://es2.php.net/FAQ.php">PHP FAQ</a></li>
<li> <a
href="http://es.php.net/manual/en/tutorial.firstpage.php">PHP
Tutorial</a></li>
<li> <a href="http://es.php.net/manual/en/index.php">PHP
Manual</a></li>
</ol>
<form action="action.php" method="post">
<p>Your name: <input type="text" name="name" /></p>
<p>Your age: <input type="text" name="age" /></p>
<p><input type="submit" /></p>
</form>

</body>
</html>
------------------------------------

This causes the follwoing output:

Warning: Cannot modify header information - headers already sent by
(output started at c:\proyectos\websites\test\index.php:4) in
c:\proyectos\websites\test\index.php on line 6

And it's correct. The headers are output as soon as ANY text is sent to
the browser - in this case the headers were sent at the first line of
your page:

<html>

Put the setcookie code immediately at the top of your file, and ensure
there is no white space (or anything else) before it.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jan 17 '06 #6
Fernando Rodríguez wrote:
OK, here's the full code:

-----------------------------------
<html>
<head>
<title>Untitled Document</title>
<?
$cookieValue = 'abra cadabra, pedo de cabra';
setcookie('aTestCookie', $cookieValue, time() + 3600);
?>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <snip> ------------------------------------

This causes the follwoing output:

Warning: Cannot modify header information - headers already sent by (output
started at c:\proyectos\websites\test\index.php:4) in c:\proyectos\websites\test\index.php
on line 6


try

-----------------------------------
1 <?php
2 $cookieValue = 'abra cadabra, pedo de cabra';
3 setcookie('aTestCookie', $cookieValue, time() + 3600);
4 ?>
5 <html>
....
I started the code with "<?php" because, instead of "<?", it will work
on all servers irrespective of their configuration. Your PHP start tag
needs a specific configuration (short_open_tags IIRC) to work.

--
If you're posting through Google read <http://cfaj.freeshell.org/google>
Jan 17 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: AHN | last post by:
Please tell me somebody what causes the cookie set with <% Response.Cookies("blah") = "Blah blah" Response.Cookies("blah").Expires = DateAdd( "h", 1, Now() ) %> work as supposed on my local...
10
by: Tony Archer | last post by:
Gentlemen, your urgent help is needed in solving a STRANGE problem. I have two servers a dev box, and a production box. On the dev box everything works fine, in production it hoses: One of...
1
by: Tony Archer | last post by:
(Forgive the dupicate post, I had left out the OS Version out of the prior post.) Gentlemen, your urgent help is needed in solving a STRANGE problem. I have two servers a dev box, and a...
2
by: swp | last post by:
I have a site created using ASP, HTML, and JavaScript. I use frames to manage a few things, and requires SSL to connect. The site is on a secured network, not meant to be cross-browser...
1
by: Joe | last post by:
Hi folks, Been looking at this for the day so any ideas would be appreciated. Here's the scenario, bear with me - it looks more complicated than it is :) I have a page templating system that...
1
by: Skeptical | last post by:
Greetings, This question is related to Microsoft Reporting Services Web service. I tried asking it in RS groups but no one seems to be knowledgeable enough to answer, so I wanted to try my...
2
by: peter | last post by:
Hi, I have very strange situation but first description ;) I have: 1) project in VB.NET, in this f.e. 1 function: Public Function Login(ByVal UserName As String, ByVal UserPassword As...
1
by: Blackstar | last post by:
I have a website I am trying to download a file from. This website is strange in the fact that there is no direct link to the URL of the file. The way the site works is as follows: 1) Enter in...
1
by: tohnsm | last post by:
Hi. I have been trying to create a cookie container. But the cookie I have stored is not correct. Below is the code: request = CType(WebRequest.Create("....."), httpWebRequest) ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.