0

This is my code:

<?php
$value = 'something from somewhere';
setcookie("TestCookie", $value);
?>

I am getting the error message as follows:

Warning: Cannot modify header information - headers already sent by (output started at /home/nairsoft/public_html/page1.php:2) in /home/nairsoft/public_html/page1.php on line 4

I am using a free webhosting server.

gvlasov
  • 18,638
  • 21
  • 74
  • 110
  • Is this the entire code in your PHP file? If then also check if you have any spaces after the closing `?>` tag. Sometimes having extra spaces can causes issues like this. – Virendra Mar 05 '15 at 04:37

3 Answers3

1

The headers are sent at the beginning of a request before the body of the server's response. It looks like your code already sent the headers so you can't modify them. As Afaan suggested, make sure you don't ouput anything before the opening php tag.

In general, it is a good idea to process everything you need at the beginning of the request before sending any of the response.

Faore
  • 21
  • 2
1

The above problem is solved by saving my code in ANSI instead of UTF-8

0

Cookie is a part of headers and headers must be sent before any actual output to the browser. Make sure that you set your cookies and other headers before you output anything. It has nothing whatsoever to do with what type of hosting package you have.

Afaan Bilal
  • 333
  • 3
  • 15