easiest filter

This is a discussion on "easiest filter" within the PHP Forum section. This forum, and the thread "easiest filter are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Aug 23rd, 2007, 00:50
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Wink easiest filter

what is the quickest way to filter user input so that malicious code can not be saved?
thanks
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Aug 23rd, 2007, 00:52
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: easiest filter

also how do you check if a certain character or phrase is in a variable.
e.g. check if the following has "123" in it.
PHP: Select all

$v "hello 123"
thanks
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Aug 23rd, 2007, 06:13
Highly Reputable Member
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 593
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: easiest filter

You could use strpos or strstr or the regex functions.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Aug 23rd, 2007, 06:48
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: easiest filter

Thanks for the link
anything on automatic filter?
and how do I get this to work :
PHP: Select all

 <?php
$string 
"<script>";
$list = array("<",">","lol");
$check ereg($list$string);
if (
$check == true) {
echo 
"true";
} else {
echo 
"false";
}    
?>
i need it to check everything in the array $list,
and I don't want to do it manually ($list[0], $list[1])
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old Aug 23rd, 2007, 09:12
Up'n'Coming Member
Join Date: Aug 2007
Location: Bicester
Posts: 70
Thanks: 0
Thanked 0 Times in 0 Posts
Re: easiest filter

Alex

Code: Select all
$list = array("<",">","lol");
$check = ereg($list, $string);
Ok. good try. ereg takes a pattern and a string. The pattern is expressed as a string so $list should be a string but you have it here as an array. (That is your mistake).

If you want to check if <, > or 'lol' are in a string you could try:

$check = ereg("(<|>|lol)", $string, $res);

The first param is a regular expression: these are difficult. Here I am trying to search for < > or lol - the | indicates or. Basically you have to build a regular expression for your match. if you just wanted to match 'lol' it would be "lol" . To match all digits it would be "\d". It gets complicated.

You can still check for $check is true to see if you had a match - but using the third param puts the actual match into an array. (If you need to use this you could perhaps look up ereg() on php.net to see some examples).

i haven't been able to test my solution but let me know if you still have a problem

re. filters: in fact you need to use regular expressions and substitution preg_replace() does the trick. Works much as ereg() but with replacing.

Justin
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6  
Old Aug 23rd, 2007, 09:21
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: easiest filter

okay.
for filtering user input,
would htmlentities be okay?
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #7  
Old Aug 23rd, 2007, 09:43
Up'n'Coming Member
Join Date: Aug 2007
Location: Bicester
Posts: 70
Thanks: 0
Thanked 0 Times in 0 Posts
Re: easiest filter

no. I think that is something to do with encoding certain characters such as < in the html entity ref form. not to do with filtering

what exactly do you want to do -can you explain a bit more? or give me an example? I was assuming you meant filtering input from a form to check it has no malicious code it? if so then something like this does it:

$safedata = preg_replace("eval","",$userInput);

This would replace any word 'eval' in $userInput with an empty string i.e get rid of it. The tricky bit is what to use as the expression to match. What characters do you want to test for?

Justin
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #8  
Old Aug 23rd, 2007, 10:05
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: easiest filter

say it's a registration form.
i'd only want numbers, letters, hyphens, underscores, full-stops and @s
how would i do that
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #9  
Old Aug 23rd, 2007, 11:03
Up'n'Coming Member
Join Date: Aug 2007
Location: Bicester
Posts: 70
Thanks: 0
Thanked 0 Times in 0 Posts
Re: easiest filter

ok

Code: Select all
 
$test = preg_match("/^([a-zA-Z0-9\-\.\_\@]+)$/", $userInput, $matches);
 
if ($test) {
$matches[0] ; // this now contains the user data which matched the pattern and is safe to use
}
else
{
//there was invalid data.
}
The pattern should match your list. It would be worth looking at a regular expression tutorial. The () says remember what is matched and put in into the first element of the array $matches. So if it matches (e.g. a-z says match any letter) it will be in the $matches[0] variable and safe to use.

I can look at this again later if you like

Justin
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #10  
Old Aug 23rd, 2007, 11:06
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: easiest filter

okay so how would i go about getting a variable 'username' using post and inserting it into mysql via that filter
I don't know how to piece it together.
thanks so far though
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #11  
Old Aug 23rd, 2007, 11:16
Up'n'Coming Member
Join Date: Aug 2007
Location: Bicester
Posts: 70
Thanks: 0
Thanked 0 Times in 0 Posts
Re: easiest filter

ok. Alex. I'll look at this tonight

Justin
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #12  
Old Aug 23rd, 2007, 11:16
Up'n'Coming Member
Join Date: Aug 2007
Location: Bicester
Posts: 70
Thanks: 0
Thanked 0 Times in 0 Posts
Re: easiest filter

(if no one else tells you by then)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #13  
Old Aug 23rd, 2007, 11:21
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: easiest filter

Okay
thanks for the help so far
*reps you*
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #14  
Old Aug 23rd, 2007, 17:10
Highly Reputable Member
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 593
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: easiest filter

PHP: Select all

if (preg_match("/^([a-zA-Z0-9\-\.\_\@]+)$/"$userInput$matches)) {
 
$matches[0] ; // this string now contains the user data which matched the pattern and is safe to use so you can insert this into your db
  
mysql_query("INSERT INTO user_table (username) VALUES('".$matches[0]."'");
} else {
 
//there was invalid data and you can't insert

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #15  
Old Aug 23rd, 2007, 21:44
Up'n'Coming Member
Join Date: Aug 2007
Location: Bicester
Posts: 70
Thanks: 0
Thanked 0 Times in 0 Posts
Re: easiest filter

Hi Alex

This is it:

Code: Select all
<html>
<head>
<style type="text/css">
body {font-family:arial,sans-serif;}
</style>
</head>
<body>
<?php
 
ini_set("display_errors","1"); //take out before production. does not work if script has fatal errors
 
if ($_SERVER['REQUEST_METHOD'] == 'GET' ) {
showPage();
}
else
{
processForm();
}
 
function showPage()
{
print "Please enter your  data";
print "<form action=\"example.php\" method=\"POST\" >";
print "<textarea cols=\"10\" rows=\"10\" name=\"userInput\" > </textarea>";
print "<br /><input type=\"submit\" >";
print "</form>";
}
 
 
function processForm()
{
$userInput = $_POST['userInput'];
 
 
$res = preg_match_all("/([a-zA-Z0-9\_\-\.\@]+)/",$userInput,$matches);//$res is number of matches
$safeUserData="";
//preg_match_all puts matches into a multidimensional array
//print $res;
//print_r($matches);
//matches[0] is iteself an array with each element being one full match
foreach ($matches[0] as $val) {
$safeUserData = $safeUserData . $val[0];
}
 
if ($res>=1) {//there was at least one  match
 
 
//do database stuff - in fact better to put in a separate function
//and connection string outside root as well
$link = mysql_connect('localhost', 'root', 'secret');
mysql_select_db ('test');
$result = mysql_query("INSERT  test values ('$safeUserData')") or die(mysql_error());
print "Your data was put into the database";
//nb - we may have stripped out tainted data
}
else
{
print "Your input did not contain any valid data";
}
 
}
?>
</body>
</html>
I made some changes so please take care.

Codepunk was wrong. If the regular expression did not match it doesn't mean that it contained invalid data it means it just didn't make any matches. If it contained some valid characters and some invalid you will still get matches . The basic idea is we are matching the characters you want and keeping them. This is different from checking for characters we don't want.

The two hard bits are the regular expression itself:
Code: Select all
/([a-zA-Z0-9\_\-\.\@]+)/
and the way using preg_match_all we get an array of arrays.

The regular expression uses a character class [] to test for a-z lower and upper case, 0-9 and the other chars you wanted (note the escape \ before them).

I''ll leave you to figure out the multi-dimensional array

ok. that's it on this one i think.

Good luck

Justin

Last edited by Kropotkin; Aug 23rd, 2007 at 21:47.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #16  
Old Aug 24th, 2007, 06:03
Highly Reputable Member
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 593
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: easiest filter

ok, what he said.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #17  
Old Aug 24th, 2007, 15:44
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: easiest filter

thanks guys I'll look over that tonight
-reps both-
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Tags
filter, filtering

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On