Jump to content

Warning: Cannot modify header information -


jackcoder

Recommended Posts

Hello, 

 

I have an error on multiple pages which looks much like this. If anyone can help that would be gratefully appreciated. I would also like to ask if you may give me an explanation as to why it is occurring and not just a solution. Also when I take the second header out the error disappears.  

 

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\blog\blog_display_post.php:47) in C:\xampp\htdocs\blog\comments.php on line 49

 

 

Here is my code:

 

if(strlen($email) > 5){

mysqli_query($con, "INSERT INTO emails (email) VALUES ('$email')");
mysqli_query($con, "INSERT INTO comments (com_name, com_com, com_date, com_blog) VALUES ('$name', '$comment', '$date', '$postid')");
 
header("Location: index.php");
}else{
mysqli_query($con, "INSERT INTO comments (com_name, com_com, com_date, com_blog) VALUES ('$name', '$comment', '$date', '$postid')");
header("Location: index.php");
}
}
}
 

Also, why the duplicate code? You are doing the same insert into comments AND the same header() regardless of your strlen() check.

if(strlen($email) > 5){
  //do the insert for email
}
//do the insert for comment
//do redirect

Out of curiosity, what's the goal behind the strlen() check? Is it just checking that $email contains a somewhat valid email address? Perhaps you're already aware of this, but PHP has a built-in function for validating email addresses:

http://php.net/manual/en/filter.examples.validation.php

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

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.