Forum Moderators: coopster

Message Too Old, No Replies

Form validation using functions .

... yes AGAIN! I just need to pick your brain on security issues

         

le_gber

3:01 pm on Jul 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all,

let's say I have a mutli-page form. I use functions to validate each form field individually.

So I have:



<?php
require_once(form_check_functions.inc)

foreach ($_REQUEST as $Key => $Value){

($Key === "surname")? $surname = check_surname($_REQUEST['surname']) : '';
($Key === "name")? $name = check_name($_REQUEST['name']) : '';

...

}

?>

in the functions I assign the value of the request to a session if it validates (to carry it across the form pages) and unset the session if it doesn't validate (in case it's ok before and then changed).

If all the form field have a corresponding session my form was correctly field so I move on tho the next step.



I was wondered if this was a secure way of doing things and if not, how you would do it.

RonPK

10:48 pm on Jul 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't see any security holes in that method, but others may.

What strikes me is that you seem to have a bunch of functions that will be used only once, such as check_name. That creates unnecessary extra server load. Why not do the validation and the assignment to session variables in the foreach-loop?

le_gber

10:41 am on Jul 14, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi RonPK,

I use to have one very big function with all the checks in but I split it up on the advice of someone knowing PHP better than me. That way I can use them for other forms on ths site as well even if they don't include all the fields.

justgowithit

1:08 pm on Jul 14, 2006 (gmt 0)

10+ Year Member



It was always suggested to me to end includes in '.inc.php' for that little added security.

If compromised a file ending in ‘.inc’ will show all – a file ending in ‘.inc.php’ will not.

Sekka

1:24 pm on Jul 14, 2006 (gmt 0)

10+ Year Member



That's a very good point.

I keep my entire "system" in a .htaccess protected folder so it can't be comprised as easily. It's included from there into other PHP files.

le_gber

2:22 pm on Jul 14, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



re the filename ending with a .php, it's what I do as well, (never used .inc) it was just as an example :) - thanks anyway people

eeek

10:46 pm on Jul 14, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I keep my entire "system" in a .htaccess protected folder

A better way is to keep your includes completely outside of the document tree. That way a mistake in your .htaccess won't expose the files.

Sekka

11:44 am on Jul 15, 2006 (gmt 0)

10+ Year Member



True, but then I'd have to change the root access for PHP, which isn't the best of ideas.