intenseone345 Posted December 21, 2009 Share Posted December 21, 2009 Hello, can anyone tell me why this php word trap fails to work, and the trigger words get blow right past, and the comments get posted anyway? heres the php code im testing on a server. <?php $words = array( 'murmer', 'frog', 'bat', ); $continue = true; foreach ($words as $word) { if (preg_match('/\b' . $word . '\b/i', $post)) { $continue = false; exit(); } } if (!$continue) { // Bad boy! } else { // 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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/185828-help-with-php-code/ Share on other sites More sharing options...
ldb358 Posted December 21, 2009 Share Posted December 21, 2009 if im not mistaken this should work: $words = array( 'murmer', 'frog', 'bat', ); $continue = true; foreach ($words as $word) { if (preg_match('/\b' . $word . '\b/i', $post)) { $continue = false; exit(); } } if (!$continue) { // Bad boy! } else { // 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"); } } Quote Link to comment https://forums.phpfreaks.com/topic/185828-help-with-php-code/#findComment-981240 Share on other sites More sharing options...
intenseone345 Posted December 21, 2009 Author Share Posted December 21, 2009 No, it still failed to work, this time it just showed a page with all the code on it, let me ask, would permission settings have anything to do with the word validator not working? thanks this is what got displayed when i tryed it out your way. $words = array( 'murmer', 'frog', 'bat', ); $continue = true; foreach ($words as $word) { if (preg_match('/\b' . $word . '\b/i', $post)) { $continue = false; exit(); } } if (!$continue) { // Bad boy! } else { // 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"); } } Quote Link to comment https://forums.phpfreaks.com/topic/185828-help-with-php-code/#findComment-981249 Share on other sites More sharing options...
teamatomic Posted December 21, 2009 Share Posted December 21, 2009 What is $post and where does it come from? HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/185828-help-with-php-code/#findComment-981276 Share on other sites More sharing options...
intenseone345 Posted December 21, 2009 Author Share Posted December 21, 2009 It comes from a "form method post" on My comments enter and submit page. Quote Link to comment https://forums.phpfreaks.com/topic/185828-help-with-php-code/#findComment-981284 Share on other sites More sharing options...
intenseone345 Posted December 21, 2009 Author Share Posted December 21, 2009 the comment input box starts with this, <form name="text_form" action='mail.php' method='post' > and the code above would be in "mail.php" , the mail and file write works fine, Its the word filter that fails to work. thanks Quote Link to comment https://forums.phpfreaks.com/topic/185828-help-with-php-code/#findComment-981288 Share on other sites More sharing options...
KevinM1 Posted December 21, 2009 Share Posted December 21, 2009 The array you're looking to use is $_POST. Quote Link to comment https://forums.phpfreaks.com/topic/185828-help-with-php-code/#findComment-981292 Share on other sites More sharing options...
teamatomic Posted December 21, 2009 Share Posted December 21, 2009 Using your code and altering it to give some test output it works OK //$post = 'this is a whisper from a flicker'; $post = 'this is a murmer from a frog'; $words = array( 'murmer', 'frog', 'bat', ); $continue = true; foreach ($words as $word) { if (preg_match('/\b' . $word . '\b/i', $post)) { $continue = false; echo 'word found!'; exit(); } } if (!$continue) { echo 'Bad boy!'; } else { echo 'Post message'; } So, is $post a var or an array, probably an array? preg_match wont do an array. Try this: $post = $_POST['comments']; Quote Link to comment https://forums.phpfreaks.com/topic/185828-help-with-php-code/#findComment-981302 Share on other sites More sharing options...
ldb358 Posted December 21, 2009 Share Posted December 21, 2009 sounds like you forgot on my example php tags Quote Link to comment https://forums.phpfreaks.com/topic/185828-help-with-php-code/#findComment-981504 Share on other sites More sharing options...
intenseone345 Posted December 21, 2009 Author Share Posted December 21, 2009 Hello, when i tested this php script below i got the following error message, ERROR SHOWN ON BROWSER: ------------------------ 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 ------------------------ The code is in its own page just as its shown below, 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? All my attempts to get both "word filter" and "write to flat file" to work together have failed, CAN anyone show me how they would structure the code above to make them work together, They work fine as seperate scripts, But not as one whole script, im no php expert and thats why im asking anyone, remove what you think needs to go. :'( 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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/185828-help-with-php-code/#findComment-981875 Share on other sites More sharing options...
ldb358 Posted December 21, 2009 Share Posted December 21, 2009 you cant echo anything out before you use a header, just remove echo 'Post message'; Quote Link to comment https://forums.phpfreaks.com/topic/185828-help-with-php-code/#findComment-981923 Share on other sites More sharing options...
intenseone345 Posted December 22, 2009 Author Share Posted December 22, 2009 I tried removing echo 'Post message'; then i got a "unexpected end error", also i wanted to also get rid of the if (!$continue) { echo 'Bad boy!'; but still get the same ERRor message, I think its the way im ending one script and entering the other, got to do it without errors and get rid of the echo's Quote Link to comment https://forums.phpfreaks.com/topic/185828-help-with-php-code/#findComment-981938 Share on other sites More sharing options...
intenseone345 Posted December 22, 2009 Author Share Posted December 22, 2009 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 } ------------------------------------- Thousand different paths, So many sterile ends, I chose the Devil's path, Never shall the sun kiss my face, And caress me with it's burning light, For I dwell in the shadows, And sleep side by side with death.... ------------------------------------- Quote Link to comment https://forums.phpfreaks.com/topic/185828-help-with-php-code/#findComment-982001 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.