I am working on a game that needs to be safe from injections, and I was wondering if this was sufficient for protection:
PHP Code:
function clean_field($i)
{
if (substr_count_array($i, array("#", "--"))) {
log_hack($i);
}
if (!preg_match('/^[a-zA-Z0-9]+$/', $i)) {
//$errors = "Invalid characters: $i";
$problem = TRUE;
}
if($problem){
return FALSE;
}
else
{
return TRUE;
}
}
function log_hack($data)
{
$timestamp = date('d/m/Y H:i:s');
$ip = $_SERVER['REMOTE_ADDR'];
$handle = fopen("hack_attempts.php", 'a+');
fwrite($handle, "$timestamp|| $data|| $ip\n");
fclose($handle);
}
function substr_count_array( $haystack, $needle ) {
$count = 0;
foreach ($needle as $substring) {
$count += substr_count( $haystack, $substring);
}
return $count;
}
Any suggestions to make it better are welcome.
Bookmarks