To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > PHP Help > Code Critique

Code Critique Having someone critique your code is always a great way to hone the skills. Stop in and post your code to see what your peers may have done differently.

Reply
 
Thread Tools Rate Thread Display Modes
Old 10-01-2003, 08:41 PM   #1
tbach2
Senior Member
 
tbach2's Avatar
 
Join Date: Jun 2003
Posts: 235
filtering user input

We all know we shouldn't trust user input. Any problems with this function or easier ways to do it?
PHP Code:
// cleanse variables

function assign($variable,$type,$restrictions) {
    
$temp='';
    switch(
$type) {
        case
'get':        $temp = $_GET[$variable]; break;
        case
'post':        $temp = $_POST[$variable]; break;
        case
'request':    $temp = $_REQUEST[$variable]; break;
        case
'cookie':        $temp = $_COOKIE[$variable]; break;
    }
    switch(
$restrictions) {
        case
'alpha':        preg_match("/([a-zA-Z ,\.]+)/",$temp,$match); break;
        case
'alphanum':    preg_match("/([a-zA-Z0-9 ,\.]+)/",$temp,$match); break;
        case
'num':        preg_match("/([0-9]+)/",$temp,$match); break;
        case
'email':        preg_match("/^(([a-zA-Z0-9_-]*\.*)*[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+)/",$temp,$match); break;
        case
'blob':        $match[1] = $temp; break;
    }
    if(
$temp!='') {
        global $
$variable;
        $
$variable = $match[1];
        return
true;
    }
    return
false;
}

// suppose you want to strip any non-alphanumeric stuff out of $_POST['username']

assign('username','post','alphanumeric');
tbach2 is offline   Reply With Quote
Old 10-01-2003, 09:21 PM   #2
Moonglobe
Better fan than rebelo!
 
Moonglobe's Avatar
 
Join Date: Apr 2003
Location: brain://localhost:left-side
Posts: 2,381
you could add support for '_' and '-'. many people use these in their input and it would be wise to allow them in alpha & alphanum.
__________________
there's no place i can be, since i found serenity.
Moonglobe is offline   Reply With Quote
Old 10-01-2003, 09:29 PM   #3
Merve
black sheep with red wool
 
Merve's Avatar
 
Join Date: Jul 2003
Location: North of the 49th parallel
Posts: 2,579
htmlentities() is also a good idea so that people can't execute HTML to get porn pics displayed from another site or something dumb like that...there's worse.
__________________
"A proof is a proof. What kind of a proof? It's a proof. A proof is a proof. And when you have a good proof, it's because it's proven." -- Jean Chrétien

The Three C's
Merve is offline   Reply With Quote
Old 10-01-2003, 09:31 PM   #4
Moonglobe
Better fan than rebelo!
 
Moonglobe's Avatar
 
Join Date: Apr 2003
Location: brain://localhost:left-side
Posts: 2,381
Merve you say that a lot now i've noticed


but anyway why would he need to? he's not allowing < or > in his regex's....
__________________
there's no place i can be, since i found serenity.

Last edited by Moonglobe; 10-01-2003 at 09:35 PM.
Moonglobe is offline   Reply With Quote
Old 10-01-2003, 09:33 PM   #5
Merve
black sheep with red wool
 
Merve's Avatar
 
Join Date: Jul 2003
Location: North of the 49th parallel
Posts: 2,579
People can type greater than or less than signs without typing &gt; or &lt;. I don't see where they are in his PCREs.

And yes I do say that a lot. A lot of people forget it.
__________________
"A proof is a proof. What kind of a proof? It's a proof. A proof is a proof. And when you have a good proof, it's because it's proven." -- Jean Chrétien

The Three C's
Merve is offline   Reply With Quote
Old 10-01-2003, 09:36 PM   #6
Moonglobe
Better fan than rebelo!
 
Moonglobe's Avatar
 
Join Date: Apr 2003
Location: brain://localhost:left-side
Posts: 2,381
ok so since when did the board start rejecting my entity references......

andway i meant < and >. the above post has been edited. all i'm saying is that HTML can't get it, it would be caught by the regexes.
__________________
there's no place i can be, since i found serenity.
Moonglobe is offline   Reply With Quote
Old 10-01-2003, 09:46 PM   #7
Merve
black sheep with red wool
 
Merve's Avatar
 
Join Date: Jul 2003
Location: North of the 49th parallel
Posts: 2,579
Please forgive me for being so stubborn Moonglobe, but I don't see < or > in his regexes...if there's something about PCRE that I don't know about that I'd really like to know.
__________________
"A proof is a proof. What kind of a proof? It's a proof. A proof is a proof. And when you have a good proof, it's because it's proven." -- Jean Chrétien

The Three C's
Merve is offline   Reply With Quote
Old 10-01-2003, 09:47 PM   #8
Moonglobe
Better fan than rebelo!
 
Moonglobe's Avatar
 
Join Date: Apr 2003
Location: brain://localhost:left-side
Posts: 2,381
Quote:
Originally posted by Merve
Please forgive me for being so stubborn Moonglobe, but I don't see < or > in his regexes...if there's something about PCRE that I don't know about that I'd really like to know.
that's the point...... they're not there. what IS there is what's allowed. that's it.
__________________
there's no place i can be, since i found serenity.
Moonglobe is offline   Reply With Quote
Old 10-01-2003, 09:58 PM   #9
Merve
black sheep with red wool
 
Merve's Avatar
 
Join Date: Jul 2003
Location: North of the 49th parallel
Posts: 2,579
I apologise Moonglobe for my stubbornness and stupidity. I should have taken the time to read over the code. Actually, I don't think it can be improved, great code! But one must admit, htmlentities() rocks!

Sorry...but it does...and if you agree with me...well too bad!

__________________
"A proof is a proof. What kind of a proof? It's a proof. A proof is a proof. And when you have a good proof, it's because it's proven." -- Jean Chrétien

The Three C's
Merve is offline   Reply With Quote
Old 10-01-2003, 09:59 PM   #10
Moonglobe
Better fan than rebelo!
 
Moonglobe's Avatar
 
Join Date: Apr 2003
Location: brain://localhost:left-side
Posts: 2,381
Quote:
Originally posted by Merve
Sorry...but it does...and if you agree with me...well too bad!
but i do agree with you....



__________________
there's no place i can be, since i found serenity.
Moonglobe is offline   Reply With Quote
Reply

Bookmarks


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

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
Forum Jump


All times are GMT -4. The time now is 05:03 AM.






Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.