why doesn't default values for this function work - resending because of bad formatting
| From: | anders thoresson | Date: | Tue, 05 Aug 2003 17:06:42 +0000 |
| Subject: | why doesn't default values for this function work - resending because of bad formatting | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-158317@lists.php.net to get a copy of this message | ||
Hi,
I'm having problem with a function that I'll use to validate user input before passing it to MySQL. For strings, I want to make sure that they aren't to long, so I have written this function:
function secure_string($unsafe_string, $max_length = -1, $errormessage = "Too many characters." ) { // verify that string isn't longer then $max_length, if $max_length is set if ($max_length > -1) { if (!is_int($max_length)) { error("Variable max_length is not an integer." ); } if (strlen($unsafe_string) > $max_length) { error($errormessage); } } [... and the validation will continue here.]
When I want to use the max length check I pass a value to the function like this:
$a_header = secure_string($_POST['a_header'], 60, "Header must not be more then 60 characters." );
But I having to problems:
1) If no max length is passed, and $max_length gets the value -1, the if- loop if ($max_length > -1) is still run.
2) Calls to my own function error doesn't work. Instead of creating a popupwindow with javascript (which works in other places where error() is called) the errormessage is printed like html.
What's wrong?
Best regards,
--
anders thoresson