Jump to content

Filtering Data


Buyocat

Recommended Posts

I've got a question which is more aimed at drumming up some opinions/starting a discussion then looking for a "right answer".  I am writing an application and I wanted to hear some suggestions on how to handle the filtering of data from the user.  The data is all coming in through one class where it is then passed as arguments to methods down the hierarchy, so it seems to make sense to do the checking at that top point.

 

However what isn't as clear is whether the regular expressions (or some alternative) used should be exclusively set by the top level class.  For instance I see that Cake (a framework) has a protected _validation array which Models can set with their own regex.

 

I am not a huge fan of this approach because it leaves the array protected, and because it may be responsibility that goes past the role of the class.  In fact I'd prefer a method which didn't rely so heavily on regex in the first place as I find regex hard to maintain.

 

So what solutions have you come up with to handle input?  I'm looking for suggestions which are flexible enough to accomodate unforseen changes in security, but simple enough to be implemented in a single class (or a single method).

 

To kick this off here's my current solution (somewhat described above)

	if (!is_array($this->_validation))
		throw new Exception("Unable to validate data bad validation hash found");

	$result = array();
	foreach ($arguments as $key => $value)
	{
		$pattern = $this->_validation[$key];
		if ($pattern == null)
			throw new Exception("The submitted data has no corresponding pattern and can not be evaluated");
		if (!preg_match($pattern, $value))
			throw new Exception("The submitted data was invalid");
		$result[$key] = $value;
	}
	return $result;

 

Thanks for any help with the brainstorming!

Link to comment
https://forums.phpfreaks.com/topic/45017-filtering-data/
Share on other sites

Sorry I didn't Understand whats your actual problem. Please Specify whats the error with which you are fighting and write short.

I just understood its something like form validation

With respect to that if you use zigmoyd validator this would be the syntax

HTML

----

<input type="text" name="frm_name" />
<input type="text" name="@name_email" /><!-- @ for email -->
<input type="password" name="#psw" /><!-- # hashed SHA1 Psw -->
<input type="password" name="^#psw"><!-- Psw Confirmation -->
<input type="text" name="!optional"><!-- ! Optional Field -->
<input type="text" name="sign(4-"><!-- 4 to 8 Char Long-->
<input type="text" name="web_site{.}+"><!-- Must Contain <dot> -->
And Many More

PHP

----

<?php
$validate = new validate("get");//if Used new validate() It will detect the form method automatically
echo "<pre>";
print_r($validate->report());//Holds the reports
echo "</pre>";
?>

And I think You have to type Less Php Codes Here

ERROR

------

It would fire thease types of errors (invalid, blank, or different)

more info

Link to comment
https://forums.phpfreaks.com/topic/45017-filtering-data/#findComment-218622
Share on other sites

I'm simply talking about security and making sure that incoming data is clean.  I am not really interested in writing a new or large system to address this issue, but I wanted to hear what other people were doing so I could pick the from best of other solutions.

 

The solution I am most familiar with is the one Cake uses or some variation on it.  You have an associative array of regex and you submit values in the $_POST (or some such array) with keys that correspond to regex to some evaluation.

Link to comment
https://forums.phpfreaks.com/topic/45017-filtering-data/#findComment-218710
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.