954,585 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Make entry field secure

Hi all,

Pretty new to PHP and I am trying to get a handle on securing a form fields data. For example, I have a simple form where user enters information into a field called mydata. The field must be able to contain multiple words and basic punctuation. What I have now is shown below in the code. Is this "really" secure? I have a good feeling that my site will become a target. Also, what should I use inplace of eregi as I just saw where that is deprecated in php5# and removed in PHP6+

//Function to sanitize values received from the form.
function clean($str) {
	$str = @trim($str);
	if(get_magic_quotes_gpc()) {
		$str = stripslashes($str);
	}
                $str = strip_tags($str)
                $str = htmlspecialchars($str,ENT_QUOTES);
	return mysql_real_escape_string($str);
}

//limit characters allowed in fields
        if (eregi("[^#-?.,!a-zA-Z0-9 ]", $_POST['mydata'])){
            $errmsg_arr[] = "non allowed character found";
            $errflag = true;
        }

$mydata = clean($_POST['mydata']);


Any help would be very much appreciated. Not sure what to even look for as I am not a hacker, nor do I even have that mentality to even "think" what is vulnerable. Thanks.

andym67
Newbie Poster
14 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

A) Don't use ereg anything.

B) http://php.net/filter_input

ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: