Jump to content
xisto Community
Sign in to follow this  
tinoymalayil

Php Error: Warning: Cannot Modify Header Information

Recommended Posts

Hi all,

Last day while i am learning php, I got one error when i attempt to setcookie in php.The following is the error and the code..

 

Error:

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\php_sandbox\cookies.php:7) in C:\xampp\htdocs\php_sandbox\cookies.php on line 10

 

Code:

<html>
<head>
	<title>Cookies</title>
</head>
<body>

	<?php // Setting a cookie

		// setcookie(name, value, expiration);
		setcookie('test', 45, time()+(60*60*24*7));
	?>

</body>
</html>

 

How can i resolve this..php file is also attached

 

Regards

cookies.php

Share this post


Link to post
Share on other sites

Why dont you just try removing the Head part from your code? It should work in that case.

<html><body>

<?php // Setting a cookie

// setcookie(name, value, expiration);
setcookie('test', 45, time()+(60*60*24*7));
?>

</body>
</html>


Share this post


Link to post
Share on other sites

All headers should be written before PHP parses any data, so you should set a cookie before any output..

Or you can use output buffering ob_start() and in that way no data will be send until the script executed and you're able to put cookies and send other headers in any place of your script you want.

But it's still recommended to write your scripts that all headers would be send BEFORE any OUTPUT :) In your case, output is HTML.

Sometimes those errors appear and it takes a long time to find there the hell is the output coming from, sometimes you can make mistake and in a class or a function file you put php end tag and push enter, example:

<?php...?>

Sp PHP sends it as a newline.. so personally I in class or function php files never put the ?> php end tag, it's a good practice. :P

<?php..

So for your example to work, you need to do:

<?php
// Setting a cookie
setcookie('test', 45, time()+(60*60*24*7));
?>
<html>
<body>
..
</body>
</html>


The same is with header(); just be sure you don't have any output before this example.. I also recommend to start using output buffering, as you can use ob_gzhandler and gzip your output and that way you can save bandwidth :)
Edited by Quatrux (see edit history)

Share this post


Link to post
Share on other sites

All headers should be written before PHP parses any data, so you should set a cookie before any output..
Or you can use output buffering ob_start() and in that way no data will be send until the script executed and you're able to put cookies and send other headers in any place of your script you want.

But it's still recommended to write your scripts that all headers would be send BEFORE any OUTPUT :) In your case, output is HTML.

Sometimes those errors appear and it takes a long time to find there the hell is the output coming from, sometimes you can make mistake and in a class or a function file you put php end tag and push enter, example:

<?php...?>

Sp PHP sends it as a newline.. so personally I in class or function php files never put the ?> php end tag, it's a good practice. :P

<?php..

So for your example to work, you need to do:



The same is with header(); just be sure you don't have any output before this example.. I also recommend to start using output buffering, as you can use ob_gzhandler and gzip your output and that way you can save bandwidth :)

It's not working after removing header part or by inserting at the top.It shows the same error.Do you have any other method to resolve this.
Edited by tinoymalayil (see edit history)

Share this post


Link to post
Share on other sites

My way should have worked.. Anyway, at the start of your script, use ob_start(); this will definitely need to work.

This is an example, of php file:

<?phpob_start();?><html><head><title>Cookies</title></head><body>This sets a cookie<?php // Setting a cookie// setcookie(name, value, expiration);setcookie('test', 45, time()+(60*60*24*7));?></body></html><?phpob_end_flush();?>

Besides, removing the <head> is just HTML and it has nothing to do with PHP setting cookies, you need to set cookies or send headers before any output..

If it doesn't work, it means you have send somekind of an output, just look at the source, maybe there's a new line or space or a tab?
Edited by Quatrux (see edit history)

Share this post


Link to post
Share on other sites

Hi all,

Last day while i am learning php, I got one error when i attempt to setcookie in php.The following is the error and the code..

 

Error:

 

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\php_sandbox\cookies.php:7) in C:\xampp\htdocs\php_sandbox\cookies.php on line 10

 

Code:

 

<html>

<head>

<title>Cookies</title>

</head>

<body>

 

<?php // Setting a cookie

 

// setcookie(name, value, expiration);

setcookie('test', 45, time()+(60*60*24*7));

?>

 

</body>

</html

 

How can i resolve this..php file is also attached

 

Regards

 


cookies are sent via HTTP header, and you already sent header by HTML tags. therefor you can delete HTML tags and make sure that there is no any white space before PHP tag ( because white space turns into echo statement that prints out a blank).

 

now your file should be like that:

 

<?php // Setting a cookie// setcookie(name, value, expiration);setcookie('test', 45, time()+(60*60*24*7));?>

and save file as php, i mean the extension should be .php, good luck.

Share this post


Link to post
Share on other sites

My way should have worked.. Anyway, at the start of your script, use ob_start(); this will definitely need to work.
This is an example, of php file:

<?phpob_start();?><html><head><title>Cookies</title></head><body>This sets a cookie<?php // Setting a cookie// setcookie(name, value, expiration);setcookie('test', 45, time()+(60*60*24*7));?></body></html><?phpob_end_flush();?>

Besides, removing the <head> is just HTML and it has nothing to do with PHP setting cookies, you need to set cookies or send headers before any output..

If it doesn't work, it means you have send somekind of an output, just look at the source, maybe there's a new line or space or a tab?

Hi Quatrux,
Thanks for the information..Its now working.every problem resolved.using ob_start() at the beginning changes the errors.
Regards,

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.