Current location: Hot Scripts Forums » Programming Languages » PHP » Help with PHP code - Multiple Variables?


Help with PHP code - Multiple Variables?

Reply
  #21 (permalink)  
Old 10-27-09, 10:37 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
preg_grep() returns an array of values that match the given pattern. If the username is valid, there should be no match, and preg_grep() should return an empty array. (which will be treated as "false").

So the exclamation mark shouldn't be there. It'll force the script to do the exact opposite as it's supposed to.

if (preg_grep(...)) means "If the given username matches one (or more) string(s) in the bad names array".

If you want to run the code only when you actually POST a username, wrap it into this:
PHP Code:

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    
// Code here

Reply With Quote
  #22 (permalink)  
Old 11-25-09, 02:00 PM
ultimatewarrior ultimatewarrior is offline
Newbie Coder
 
Join Date: Oct 2009
Posts: 61
Thanks: 11
Thanked 0 Times in 0 Posts
I was off for a bit, sorry.

I tried this code but would only work if I given the exact match.

if ($_SERVER['REQUEST_METHOD'] == 'POST'){
$badnames = array('www', '.com', 'badword', 'http', '...');

// $username is the name the user submitted.
$username = trim($_POST['message']);

$tmp = strtolower($username);
$tmp = preg_quote($tmp, '~');

if (preg_grep("~{$tmp}~", $badnames)){
echo Message("Sorry. The system thinks you are spamming, sometimes this is wrong.");
include "footer.php";
die();
}
}
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP Image Gallery showing headers and code. Apache problem? slaterino Web Servers 0 08-08-08 05:33 AM
PHP Image Gallery showing headers and code slaterino PHP 0 08-08-08 05:32 AM
How to write 'elegant' code in php? aditya2071990 PHP 7 08-05-08 06:55 AM
Require Forum website project code in php language suaveshiva PHP 4 04-30-07 03:21 PM
PHP code formatter... jumbo1 The Lounge 1 03-19-07 12:01 PM


All times are GMT -5. The time now is 08:00 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.