How to solve Warning: - Cannot modify header information - headers already sent by in PHP.


5-How to solve Warning: - Cannot modify header information - headers already sent by in PHP.

Hello friends, now we talking about most common problem in PHP cannot modify header information it’s appear in old MYSQL database  version when we parse some HTML value iN PHP before redirect header. It time database confuse and give error like this.


Warning: Cannot modify header information - headers already sent by (output started at /home/website/public_html/index.php:30) in /home/website/public_html/index.php on line 145


And its cant redirect header to specified location or page to solve this problem we are using this lline of code :-


<?php ob_start(); ?>


This function start the data buffering in PHP MYSQL database and redirect successfully. Example:-


<?php

ob_start(); // start the buffring data

session_start(); // start the session


if (isset($_POST[\\\'post_title\\\'])) {

 $post_title = mysql_real_escape_string($_POST[\\\'post_title\\\']);

$post_body = mysql_real_escape_string($_POST[\\\'post_body\\\']);


$sql = mysql_query(\\\"INSERT INTO your_table (post_title, post_body)  VALUES(\\\'$post_title\\\',\\\'$post_body\\\')\\\") or die (mysql_error());

header(\\\"location: index2.php\\\"); // header location where you want to send this after insert data in database

ob_end_flush(); // end data buffer  

exit();  

} else {

echo “you cannot enter data in database”;

}

?>



This is example of the using of buffer function with php buffer function always start before session and u can end this with <?php ob_end_flush(); ?> after the header function  if you don’t want to end this function don’t be worry its work proper nothing is change.Use it as free have a nice day. remove backslash when you are using this code its educated by database we will fix it letter.



TAGS

Oct 26, 2012 • Article# 5 - by Sidharth Gupta
MYSQL DATABASE SESSION DATA BUFFE FUNCTION OB_STRT PHP MODIFY HEADER