개발이야기/PHP

[PHP] 문자열 필터링 함수 (SQL Injection 방어)

후린개발자 2023. 5. 9.
반응형

사용자가 입력한 값에 대해서 filter_SQL 함수와 SQL_Injection 함수를 사용하여 값을 보호(?) 하는 함수를 안내드립니다.

 

filter_SQL 함수는 다양한 문자열 필터링 및 HTML 엔티티 변환을 수행하고, SQL_Injection 함수를 호출하여 추가적인 SQL Injection 보호를 제공합니다. 아래 함수는 입력값에 대한 보호를 제공하기 위한 일부 보안 방식 중 하나이며, 다른 방법과 함께 사용해야 강력한 공격에 대응할 수 있습니다.

 

<?php
function filter_SQL($content){
	$content = str_replace("&", "&amp", $content); 
	$content = str_replace("<", "&lt", $content);  
	$content = str_replace(">", "&gt", $content);  
	$content = str_replace("'", "&apos", $content);   
	$content = str_replace("\"", "&quot", $content);  
	$content = str_replace("\r", "", $content);
	$content = str_replace("'", "", $content);   
	$content = str_replace('"', "", $content);  
	$content = str_replace("--", "", $content);
	$content = str_replace(";", "", $content);
	$content = str_replace("%", "", $content);
	$content = str_replace("+", "", $content);
	$content = str_replace("script", "", $content);
	$content = str_replace("alert", "", $content);
	$content = str_replace("cookie", "", $content);
	$content = SQL_Injection($content);
	return $content;
}
function SQL_Injection($get_Str) { 
	return preg_replace("/( select| union| insert| update| delete| drop| and| or|\"|\'|#|\/\*|\*\/|\\\|\;)/i","", $get_Str); 
}

$userid = filter_SQL($_REQUEST["userid"]);
?>

 

반응형

댓글

💲 추천 글