Page 1 of 1

Help with php code

Posted: Sun Dec 20, 2009 8:25 pm
by intenseone345
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
?>

Re: Help with php code

Posted: Sun Dec 20, 2009 8:40 pm
by daedalus__

Code: Select all

$words = array('murmer', 'frog', 'bat', );
??

Code: Select all

$words = array('murmer', 'frog', 'bat' );
im surprised it doesn't die with a fatal error.

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
?>
 

Re: Help with php code

Posted: Sun Dec 20, 2009 8:51 pm
by AbraCadaver
daedalus__ wrote:

Code: Select all

$words = array('murmer', 'frog', 'bat', );
??

Code: Select all

$words = array('murmer', 'frog', 'bat' );
im surprised it doesn't die with a fatal error.
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:

Code: Select all

$words = array('murmer',
               'frog',
               'bat',
);

Re: Help with php code

Posted: Sun Dec 20, 2009 8:53 pm
by daedalus__
really? i didn't know that was legal syntax. my mistake.

Re: Help with php code

Posted: Mon Dec 21, 2009 10:46 am
by intenseone345
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

Re: Help with php code

Posted: Mon Dec 21, 2009 12:57 pm
by MichaelR
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
?>