What I meant is that you should check what they submit.
Use PHP to check that the data doesn't exceed the expected length etc...
Most important thing: clean your data. If you don't, you're exposed to sql injections attack. mysql_real_escape_string() will fix all of the problems you might encounter in terms of cleaning.
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
}
Also, you will use htmlentities() when you display the data. Do not use this function to handle incoming data. Only use it once you want to display it.
htmlentities() will ensure that users can't display html code. Important note: htmlentities() doesn't deal with attributes in html tags.
Bookmarks