In: Computer Science
Why am I getting this error using 000webhost , but I do not get this error running XAMPP?
Warning: Cannot modify header information -
headers already sent by (output started at
/storage/ssd2/006/14881006/public_html/test/index.php:1) in
/storage/ssd2/006/14881006/public_html/test/index.php
on line 8
Code is below.
********************************************************************************************************************
<!DOCTYPE html>
<?php
//Determine if the submit button has been clicked
to set the cookie name and value
if (isset($_POST['name'])){
$cookie_name =
$_POST['name'];
$cookie_value =
$_POST['value'];
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
// 86400 = 1 day
} else {
$cookie_name = NULL;
$cookie_value = NULL;
}
?>
<html>
<body>
<!-- Form to get cookie name and
value -->
<form action="index.php"
method="post">
<p>Cookie
name: <input type="text" name="name" /></p>
<p>Cookie
value: <input type="text" name="value" /></p>
<br>
<p><input type="submit" name="Submit"/></p>
</form>
<?php
//Display
cookie name and value
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie
named '" .$cookie_name. "' is not set!";
} else {
echo "Cookie '"
.$cookie_name. "' is set!<br>";
echo "Value is:
" .$_COOKIE[$cookie_name];
}
?>
<p><strong>Note:</strong> You might have to
reload the page to see the value of the cookie.</p>
</body>
</html>
Some functions modifying the HTTP header are:
header
/ header_remove
session_start
/
session_regenerate_id
setcookie
/ setrawcookie
Output can be:
Unintentional:
<?php
or after
?>
Intentional:
print
, echo
and other functions
producing output<html>
sections prior
<?php
code.The header()
warning contains all relevant
information to locate the problem cause:
Cannot modify header information - headers already sent by (output started at /storage/ssd2/006/14881006/public_html/test/index.php:1) in /storage/ssd2/006/14881006/public_html/test/index.php on line 8
Here "line 8" refers to the script where the
header()
invocation failed.
The "output started at" note within the parenthesis is more significant. It denominates the source of previous output.
In this,
Raw HTML areas
Unparsed HTML sections in a .php
file are direct
output as well. Script conditions that will trigger a
header()
call must be noted before any raw
<html>
blocks.
<!DOCTYPE html>
<?php
// Too late for headers already.
Use a templating scheme to separate processing from output logic.
Another reason could be:
Whitespace before <?php
for "script.php
line 1" warnings
If the warning refers to output in line
1
, then it's mostly leading
whitespace, text or HTML before the opening
<?php
token.
<?php
# There's a SINGLE space/newline before <? - Which already seals it.
Similarly it can occur for appended scripts or script sections:
?>
<?php
PHP actually eats up a single linebreak after close tags. But it won't compensate multiple newlines or tabs or spaces shifted into such gaps.
Hope this helps you. For further queries please add comment below. Please give an upvote. Thank you.