Also, check the expected length of the data.
Here is some code to check the data:
PHP Code:
function checkIncomingData($idata, $minsize, $maxsize)
{
if (
$idata == ''
or
strlen($idata)<$minsize
or
strlen($idata)>$maxsize
)
{
return false;
}
else
{
return true;
}
}
you can add as many checks as you want. The $idata == '' part and $idata<somelength is somewhat redundant, but I keep it to display different error messages.
here is a function that cleans your data. mysql_real_escape_string is a little more torough than addslashes.
PHP Code:
//make sure that nothing bad can be entered by the user (-->sql injection attack)
function cleanIncomingData($idata)
{
$cleaned = trim($idata);
$cleaned = mysql_real_escape_string($cleaned);
return $cleaned;
}
you would typically use these functions like this:
PHP Code:
if (
!checkIncomingData($_POST['categoryname'], 1, 100)
or
!checkIncomingData($_POST['pagetitle'], 1, 100)
or
!checkIncomingData($_POST['pagetexttitle'], 1, 100)
or
!checkIncomingData($_POST['pagetext'], 1, 50000)
)
{
header('Location:' . $url . 'errormessage.php');
}
else
{
$categoryname = cleanIncomingData($_POST['categoryname']);
$pagetitle = cleanIncomingData($_POST['pagetitle']);
$pagetexttitle = cleanIncomingData($_POST['pagetexttitle']);
$pagetext = cleanIncomingData($_POST['pagetext']);
//insert in db
}
Next step will obviously be to check the expected data type etc...
Bookmarks