Page 1 of 1

Please help me with this

Posted: Mon Dec 21, 2009 1:17 pm
by intenseone345
Hello, when i tested this php script below i got the following error message,

Post message
Warning: Cannot modify header information - headers already sent by
(output started at /hermes/bosweb/web176/b1761/ipw.tomagr/public_html/maila.php:23)
in /hermes/bosweb/web176/b1761/ipw.tomagr/public_html/maila.php on line 39

Any ideas i'd love to hear, thanks

the php script:

<?
$post = $_POST['comments'];
$words = array('murmer', 'frog', 'bat', );

$continue = true;

foreach ($words as $word) {

if (preg_match('/\b' . $word . '\b/i', $post)) {
$continue = false;
header("Location: mysite.html");
exit();

}

}

if (!$continue) {
echo 'Bad boy!';
}

else {
echo 'Post message';
}
$fc = fopen("comments.txt","a+b"); //opens the file to append new comment -
fputs($fc,$_POST['comments']."\n\n\nNewComment->"); //writes the comments followed by a
fclose($fc); //closes the files

if(sizeof($_POST)) {
$body = "";
while(list($key, $val) = each($HTTP_POST_VARS)) {
$body .= "$key: $val \n";
}

mail("myemail@myemail.com", // to
"Subject Line",
$body);

header("Location: mysite.html");
}

// end form processing
?>

Re: Please help me with this

Posted: Mon Dec 21, 2009 1:23 pm
by oscardog
Is this .php code within a page?

If there is anything above the header code(HTML code, or any sort of output whatsoever) it will not run the header command. I personally find it odd that it works like this, but that's just how it is :)

Re: Please help me with this

Posted: Mon Dec 21, 2009 1:29 pm
by intenseone345
No its in its own page just as its shown, simple word check, then post the comments to a flat file and emails me with posted comment.
the, $post = $_POST['comments']; reffers to the form on another page that the comments are comming from. want to know what im doing wrong in this script? thanks

Re: Please help me with this

Posted: Mon Dec 21, 2009 1:35 pm
by oscardog
intenseone345 wrote:No its in its own page just as its shown, simple word check, then post the comments to a flat file and emails me with posted comment.
the, $post = $_POST['comments']; reffers to the form on another page that the comments are comming from. want to know what im doing wrong in this script? thanks
I see. You can't have the header set at the bottom of the page - as you have echo statememts above and if anything it output to the page(so an echo statement) the header will throw an error that you see when you run your page :)

Re: Please help me with this

Posted: Mon Dec 21, 2009 2:12 pm
by MichaelR
oscardog wrote:If there is anything above the header code [my emphasis](HTML code, or any sort of output whatsoever) it will not run the header command. I personally find it odd that it works like this, but that's just how it is
That's not entirely true. Header code will not work if anything is output to the browser. Subtle difference. If you use output buffering then echo commands and the like can be placed above any header code, because the echo statements are not output until the end of the buffering.

For example, the following will work:

Code: Select all

 
  ob_start();
 
  echo 'Hello, world!';
 
  if (isset($var)) {
    header('Location: index.php');
    exit;
  }
 
  ob_end_flush();
 

Re: Please help me with this

Posted: Mon Dec 21, 2009 4:04 pm
by intenseone345
SOLVED

Re: Please help me with this

Posted: Mon Dec 21, 2009 8:27 pm
by intenseone345
OK Solved, here is a tested and working php script to perform the following for those interested.

Basic word filter that kills comment all together if you are getting bombed with via gra adds and such, after validation, it inputs to a flat file data base and can be called to display comments in text area, i did some script to make text area always scroll to bottom to show most recent comments, And emails you with the comment submitted.

php script:

<?
$post = $_POST['comments'];
$words = array('murmer', 'frog', 'bat', ); //add your trigger words here.

$continue = true;

foreach ($words as $word) {

if (preg_match('/\b' . $word . '\b/i', $post)) {
$continue = false;
header("Location: mysite.html"); //if user enters unwanted text, script redirects
exit(); //back to comment display box without adding
//offenders comments, such as urls or via gra adds
}

}

if (!$continue) {
echo 'Bad boy!'; //this is useless here

} else {
$fc = fopen("comments.txt","a+b"); //opens the file to append new comment -
fputs($fc,$_POST['comments']."\n\n\nNewComment->"); //writes the comments followed by a
fclose($fc); //closes the files

if(sizeof($_POST)) {
$body = "";
while(list($key, $val) = each($HTTP_POST_VARS)) {
$body .= "$key: $val \n";
}

mail("myemail@myemail", // emails you with new comment in body
"Subject Line",
$body);

header("Location: mysite.html"); //refreashes back to video page were more php script
} //shows new comment in real time

}

Re: Please help me with this

Posted: Tue Dec 22, 2009 4:25 am
by MichaelR
Yes, that works. But because of the header(); code in the foreach loop, you don't need the if {} else {} block, or the $continue variable. But well done for getting it working.