
Originally Posted by
brainpipe
Can you give an example of how you are calling this function when no max length is passed? If you pass null, that if statement should evaluate to false; if you pass a string though, it will evaluate to true.
With max_length I call it like this:
PHP Code:
$a_header = secure_string($_POST['a_header'], 60, "Rubriken får inte vara mer än 60 tecken.");
and without:
PHP Code:
$a_desc = secure_string($_POST['a_desc']);
A code example would be helpful here as well, specifically the error() function and the code that calls it.
My error() looks like this:
PHP Code:
function error($msg)
{
?>
<SCRIPT language="JavaScript">
<!--
alert("<?=$msg?>");
history.back();
-->
</SCRIPT>
<?php
exit;
}
In my secure_string() which looks like this, it doesn't work:
PHP Code:
function secure_string($unsafe_string, $max_length = -1, $errormessage = "Du har skrivit för många tecken.")
{
// verify that string isn't longer then $max_length, if $max_length is set
if ($max_length > -1)
{
if (!is_int($max_length))
{
error("Variabeln max_length är inte en siffra.");
}
if (strlen($unsafe_string) > $max_length)
{
error($errormessage);
}
}
// create array containing bad words
$badwords = array(";","--","select","drop","insert","xp_","delete");
$goodwords = array(":","-","choose","leave","add"," ","remove");
// check for occurences of $badwords
for($i=0; $i<7; $i++)
{
$unsafe_string = str_replace("$badwords[$i]", "$goodwords[$i]","$unsafe_string");
}
$unsafe_string = AddSlashes($unsafe_string);
$unsafe_string = htmlentities($unsafe_string);
$unsafe_string = strip_tags($unsafe_string);
$unsafe_string = trim($unsafe_string);
Return $unsafe_string;
}
But in validate_email it works:
PHP Code:
// validate entered email address
function validate_email($unchecked_email, $errortype = 1, $errormessage = "Du har inte skrivit in en giltlig e-postadress.")
{
if(!ereg("(^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z]{2,3}$)", $unchecked_email))
{
if($errortype == 1)
{
error($errormessage);
}
Return 1;
}
}
And by the way, are the last steps in secure_string needed or not (to make it secure for mysql, or could I trim it?
PHP Code:
// create array containing bad words
$badwords = array(";","--","select","drop","insert","xp_","delete");
$goodwords = array(":","-","choose","leave","add"," ","remove");
// check for occurences of $badwords
for($i=0; $i<7; $i++)
{
$unsafe_string = str_replace("$badwords[$i]", "$goodwords[$i]","$unsafe_string");
}
$unsafe_string = AddSlashes($unsafe_string);
$unsafe_string = htmlentities($unsafe_string);
$unsafe_string = strip_tags($unsafe_string);
$unsafe_string = trim($unsafe_string);
Return $unsafe_string;
Thanks.
Bookmarks