I have this header information error while I was playing with a script.

Warning: Cannot modify header information - headers already sent by (output started at /home/user/public_html/layouts/connection.php:8) in /home/user/public_html/layouts/addcomments.php on line 23

I wanted the page to redirect itself after submitting the comment, but I messed up.
I also want to know how do I make the url text area capible of accepting urls and emails.
addcomments.php

<?php 
include('connection.php');
$action = strip_tags($_GET["action"]); 
$id = strip_tags($_GET["id"]); 

if ($action=="add") 
{ 
    $name = strip_tags($_POST["name"]); 
    $entry = strip_tags($_POST["entry"]); 
    $email = strip_tags($_POST["email"]); 
     
    // add comments 
    if (empty($name) || empty($entry)) 
    { 
        die ("Error, you cannot submit a blank entry."); 
    } 
     
    $q = "insert into layout_comments (id, name, email, date, layout_id, entry) VALUES 

('','$name','$email',now(),'$id','$entry')"; 
    $result= mysql_query($q) or die 
    ("Could not execute query : $q." . mysql_error());     
    if ($result) 
    { 
header('Location: addcomments.php');
         
    } 
     
} 
else { 


?><form action="<?php echo "addcomments.php?action=add&id='$id' "; ?>" method="post"> 
<table width="130" border="0" cellspacing="1" cellpadding="0"> 
<tr> 
<td height="23" colspan="2"><b>Add Comments</b></td> 
</tr> 
<tr> 
<td>Name:*</td> 
<td> 
<input type="text" name="name" size="30"> 
</td> 
</tr> 
<tr> 
<td height="25">Url:</td> 
<td height="25"> 
<input type="text" name="email" size="30"> 
</td> 
</tr> 
<tr> 
<td height="25">Comments:*</td> 
<td height="25"> 
<textarea name="entry" cols="30" rows="5"></textarea> 
</td> 
</tr> 
<tr><td colspan=2><input type="submit" value="submit"></td></tr> 
</table></form> 
<?php 
} 
?> 
<?
$id = strip_tags($_GET["id"]); 

$q = "SELECT * from layout_comments where layout_id='$id' order by date desc "; 
$result= mysql_query($q) or die 
("Could not execute query : $q." . mysql_error()); 


while ($row=mysql_fetch_array($result)) 
{ 
    $id=$row["id"]; 
    $name=$row["name"]; 
    $email=$row["email"]; 
    $entry=$row["entry"]; 
    $date=$row["date"]; 
    $layout_id = $row["layout_id"]; 

?> 

    <table width="80%" border="0" cellspacing="1" cellpadding="0" align="center"> 
    <tr> 
    <td>Comments for <?php echo "$title"; ?></td> 
    </tr> 
    <tr> 
    <td> 
    <?php echo "$entry"; ?> 
    <p>Posted by <a href="<?php echo "$email"; ?>"><?php echo "$name"; ?></a> on <?php 

echo "$date"; ?>.</p> 
    </td> 
    </tr> 
    </table> 

<?php 
} 


?>

Dani AI

Generated

The error trace points to output beginning inside connection.php (line 8), so the redirect attempt happens after headers were already sent. As suggested, common causes are stray whitespace before <?php or after a closing ?>, explicit output (echo/print/HTML) in the included file, or a UTF‑8 BOM at the start of the file. Open connection.php in a hex-aware editor and confirm the first byte is <?php (no BOM/no blank lines). Removing the closing ?> from pure-PHP include files eliminates trailing-newline problems. See the PHP diagnostics for headers_sent() for more detail: headers_sent().

A short, safe redirect pattern while fixing the include is to use output buffering and always stop execution after sending Location. Also prefer a root-relative or absolute Location per the HTTP spec; see the header() docs.

<?php
ob_start();
// includes and processing here
header('Location: /layouts/addcomments.php');
exit;
?>

The form has a logic bug that affects URL/email handling: the field labelled "Url" is named email, so the wrong value gets stored/displayed. Use separate inputs (e.g., name="email" and name="url") or normalize the single field before storing. Validate server-side with filter_var() and escape output with htmlspecialchars(); example:

if (filter_var($val, FILTER_VALIDATE_EMAIL)) {
  $href = 'mailto:' . htmlspecialchars($val);
} elseif (filter_var($val, FILTER_VALIDATE_URL)) {
  $url = preg_match('~^[a-z]+://~i', $val) ? $val : 'http://' . $val;
  $href = htmlspecialchars($url);
} else {
  $href = '';
}

Also avoid relying only on strip_tags() for safety, switch from the old mysql_* calls to prepared statements (mysqli or PDO) to prevent injection, and check for any undefined variables like $title when rendering comments. For validation reference see: filter_var().

In a quick glance at your code, it does not appear that your are outputting anything before you attempt the header() function. So, my guess is either:

1. connection.php is outputting something using echo(), print(), etc.
2. You have whitespace before or after the initial and final <?PHP ?> tags in the connection.php include file.

I do not understand what you are asking here:

...how do I make the url text area capible of accepting urls and emails.

It will accept a URL or email address---anything your user can type on the keyboard can be typed into that text input.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.