Help with php code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
intenseone345
Forum Newbie
Posts: 16
Joined: Sun Dec 20, 2009 3:10 pm

Help with php code

Post 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
?>
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Re: Help with php code

Post 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
?>
 
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Help with php code

Post 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',
);
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.
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Re: Help with php code

Post by daedalus__ »

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

Post 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
MichaelR
Forum Contributor
Posts: 148
Joined: Sat Jan 03, 2009 3:27 pm

Re: Help with php code

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