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
?>
Help with php code
Moderator: General Moderators
- daedalus__
- DevNet Resident
- Posts: 1925
- Joined: Thu Feb 09, 2006 4:52 pm
Re: Help with php code
Code: Select all
$words = array('murmer', 'frog', 'bat', );Code: Select all
$words = array('murmer', 'frog', 'bat' );actually.
Code: Select all
<?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) // WHY IS THIS EMPTY? WHY IS THIS EMPTY? WHY IS THIS EMPTY? WHY IS THIS EMPTY? WHY IS THIS EMPTY? WHY IS THIS EMPTY?
{
// 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
?>
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: Help with php code
Why would it? That's very common practice so that it is easy to add extra elements without forgetting a comma, though it usually looks more like:daedalus__ wrote:??Code: Select all
$words = array('murmer', 'frog', 'bat', );
im surprised it doesn't die with a fatal error.Code: Select all
$words = array('murmer', 'frog', 'bat' );
Code: Select all
$words = array('murmer',
'frog',
'bat',
);mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
- daedalus__
- DevNet Resident
- Posts: 1925
- Joined: Thu Feb 09, 2006 4:52 pm
Re: Help with php code
really? i didn't know that was legal syntax. my mistake.
-
intenseone345
- Forum Newbie
- Posts: 16
- Joined: Sun Dec 20, 2009 3:10 pm
Re: Help with php code
Ok, still dont seem work, someone told me this,
$post in the preg_match,
should be $_POST['comments']
so to try this out would do it like this,
from this old line of code,
if (preg_match('/\b' . $word . '\b/i', $post))
to this change?
if (preg_match('/\b' . $word . '\b/i', $_POST['comments']))
please let me know why such a simple script is not seeing the data flowing right past it.
thanks for the help
$post in the preg_match,
should be $_POST['comments']
so to try this out would do it like this,
from this old line of code,
if (preg_match('/\b' . $word . '\b/i', $post))
to this change?
if (preg_match('/\b' . $word . '\b/i', $_POST['comments']))
please let me know why such a simple script is not seeing the data flowing right past it.
thanks for the help
Re: Help with php code
Yes, it should be $_POST['comments'] (based on the rest of your code). And the reason it's failing is because the code which posts the comment is not contained within the else { } block. Try this:
Code: Select all
<?php
$words = array(
'murmer',
'frog',
'bat',
);
$continue = true;
foreach ($words as $word) {
if (preg_match('/\b' . $word . '\b/i', $_POST['comments'])) {
$continue = false;
exit();
}
}
if (!$continue) {
echo 'You cannot post that content.';
}
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.com", "Subject Line", $body);
header("Location: mysite.html");
}
}
// end form processing
?>