วันอาทิตย์ที่ 26 กันยายน พ.ศ. 2553

Code ป้องกัน XSS และ SQL Injection

Code ป้องกัน XSS และ SQL Injection

function is_valid($input)
{
$input = strtolower($input);

if (str_word_count($input) > 1)
{
$loop = true;
$input = explode(" ",$input);
}

$bad_strings = array("'", "--", "select", "union", "insert", "update", "like", "delete", "1=1", "or", "#", "//");

if ($loop == true)
{
foreach($input as $value)
{
if (in_array($value, $bad_strings))
{
return false;
}
else
{
return true;
}
}
}
else
{
if (in_array($input, $bad_strings))
{
return false;
}
else
{
return true;
}
}
}




วิธีนำไปใช้ก็คือ เรียกใช้ฟังก์ชั่นที่เราเขียน

if (is_valid($_POST["username"]) == true && is_valid($_POST["password"]) == true)
{
//login now
}

ไม่มีความคิดเห็น:

แสดงความคิดเห็น