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


Help with PHP code - Multiple Variables?

Reply
  #1 (permalink)  
Old 10-04-09, 12:32 PM
ultimatewarrior ultimatewarrior is offline
Newbie Coder
 
Join Date: Oct 2009
Posts: 61
Thanks: 11
Thanked 0 Times in 0 Posts
Question Help with PHP code - Multiple Variables?

Hello

This is part of my PHP code:

$badwords = "rubbish";

and another part

if($username == $badwords){

this PHP code works when you type rubbish in the username, but I want to add extra words into $badwords, but I can't, please tell me how.

if you can do this, thank you so much! thank you for saving so much time for me!


ultimatewarrior
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 10-04-09, 12:58 PM
ruteckycs's Avatar
ruteckycs ruteckycs is offline
Coding Addict
 
Join Date: Jul 2009
Posts: 377
Thanks: 6
Thanked 10 Times in 10 Posts
You need to use an array. Then loop through them:

PHP Code:

$badwords= array();
$badwords[0]="badword_here";
$badwords[1]="badword_here";
$badwords[2]="badword_here";
//do for each bad word

$i 0;
while (
$i <= count($Array)) {
  print 
"$Array[i]<br>";
 
$i++;

*This code only prints the bad words, I would have included your if statement in the code but you didnt give it all.

**This code has the bad words hard coded, if I were you I would have them saved in a DB or in a flat file even for easy updating.
__________________
This post was created with 100% recycled electrons.

Last edited by ruteckycs; 10-04-09 at 01:01 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 10-04-09, 05:06 PM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,516
Thanks: 20
Thanked 109 Times in 106 Posts
PHP Code:

$aBad=array('password','notnice','ugly', ...);  /* All lowercase */

/* Make username lowercase for comparison */
if (in_array(strtolower($slc),$aBad)) die('Bad username'); 
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 10-04-09, 05:59 PM
ruteckycs's Avatar
ruteckycs ruteckycs is offline
Coding Addict
 
Join Date: Jul 2009
Posts: 377
Thanks: 6
Thanked 10 Times in 10 Posts
First time I have heard about in_array() this is a good example of me doing things the 'hard way'. Question about it though. Is the same running time as a bunch of ifs or is this faster?
__________________
This post was created with 100% recycled electrons.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 10-04-09, 06:19 PM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 Posts
It's much faster.

Make sure to trim() the username as well.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 10-04-09, 06:51 PM
PopSmith PopSmith is offline
Newbie Coder
 
Join Date: May 2009
Posts: 18
Thanks: 5
Thanked 0 Times in 0 Posts
Is it recommended to use in_array() even if the list is somewhat lengthy, as in over 20-30 items?

I don't intend on having a list that long, because strlower() eliminates the need to check multiple variations based on capitalization, but I'm just curious.

Also, is it possible to check the input for things such as underscores or dashes between letters of "banned" words? (i.e.
user-name, user_name, u_sern_ame etc.) or would you recommend letting people use them?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 10-04-09, 07:28 PM
ruteckycs's Avatar
ruteckycs ruteckycs is offline
Coding Addict
 
Join Date: Jul 2009
Posts: 377
Thanks: 6
Thanked 10 Times in 10 Posts
20-30 is not a long list. When I said a long list in my post to wirehoper I was thinking 15,000 or so.

You can use remove any underscores
PHP Code:

preg_replace("-"""$user_name); 

You can make it to remove multiple things (underscore , dashes , dots, commas)
PHP Code:

preg_replace("-,."""$user_name); 

__________________
This post was created with 100% recycled electrons.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
The Following User Says Thank You to ruteckycs For This Useful Post:
PopSmith (10-04-09)
  #8 (permalink)  
Old 10-04-09, 07:38 PM
PopSmith PopSmith is offline
Newbie Coder
 
Join Date: May 2009
Posts: 18
Thanks: 5
Thanked 0 Times in 0 Posts
Thanks ruteckycs. I wasn't sure what was considered a "long" list and accidentally left off a zero in my post.

But if you consider something like 15,000 a long list then I won't have to worry about it for my purposes. Thanks for the help on removing characters from input via preg_replace.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #9 (permalink)  
Old 10-04-09, 11:03 PM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,516
Thanks: 20
Thanked 109 Times in 106 Posts
Nico - good point about trim.

PHP Code:

$aBad=array('password','notnice','ugly', ...);  /* All lowercase */ 

/* Make username lowercase for comparison */ 
if (in_array(strtolower(trim($slc)),$aBad)) die('Bad username'); 
If you find you have a really long list, I'd store it in a text file and use grep, on the commandline. It's unlikey you'll ever need it.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #10 (permalink)  
Old 10-05-09, 11:33 AM
ultimatewarrior ultimatewarrior is offline
Newbie Coder
 
Join Date: Oct 2009
Posts: 61
Thanks: 11
Thanked 0 Times in 0 Posts
Thanks! I will try them out! =D
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
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 06:33 AM
PHP Image Gallery showing headers and code slaterino PHP 0 08-08-08 06:32 AM
How to write 'elegant' code in php? aditya2071990 PHP 7 08-05-08 07:55 AM
Require Forum website project code in php language suaveshiva PHP 4 04-30-07 04:21 PM
PHP code formatter... jumbo1 The Lounge 1 03-19-07 01:01 PM


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