2013-02-26 2 views
-1

PHP 코드의 보호 기능을 향상시키는 데 도움을 줄 수있는 사람이 있는지 궁금합니다. 매우 기본적이고 방어가 필요합니다.PHP/MySQL 인젝션 방지

나는 내가 도움이 필요한 것에 대해 충분한 세부 사항을 제공했으면 좋겠다.

<?php 
    //This is only displayed if they have submitted the form 

    if ($searching =="yes") { 
     echo "<h2>Results</h2><p>"; 
     //If they did not enter a search term we give them an error 

     if ($find == "") 
     if ($f == "") 
     if ($info == "") 
     if ($zip == "") 
     if ($state == "") 
     if ($email == "") 
     if ($address == "") 
     { 
      echo "<p>You forgot to enter a search term"; 
      exit; 
     } 

     // Otherwise we connect to our Database 
     mysql_connect("xx.xx.xx", "xxxx", "xxxxx") or die(mysql_error()); 
     mysql_select_db("xxxxx") or die(mysql_error()); 
     // We preform a bit of filtering 
     $find = strtoupper($find); 
     $find = strip_tags($find); 
     $find = trim ($find); 
     $f = strtoupper($f); 
     $f = strip_tags($f); 
     $f = trim ($f); 
     $info = strtoupper($info); 
     $info = strip_tags($info); 
     $info = trim ($info); 
     $zip = strtoupper($zip); 
     $zip = strip_tags($zip); 
     $zip = trim ($zip); 

     $state = strtoupper($state); 
     $state = strip_tags($state); 
     $state = trim ($state); 

     $email = strtoupper($email); 
     $email = strip_tags($email); 
     $email = trim ($email); 

     $address = strtoupper($address); 
     $address = strip_tags($address); 
     $address = trim ($address); 
     //Now we search for our search term, in the field the user specified 
$data = mysql_query("SELECT * FROM users WHERE fname 
LIKE '%" . mysql_real_escape_string($find)  . "%' AND lname 
LIKE '%" . mysql_real_escape_string($f)   . "%' AND info 
LIKE '%" . mysql_real_escape_string($info)  . "%' AND zip 
LIKE '%" . mysql_real_escape_string($zip)  . "%' AND state 
LIKE '%" . mysql_real_escape_string($state)  . "%' AND email 
LIKE '%" . mysql_real_escape_string($email)  . "%' AND address 
LIKE '%" . mysql_real_escape_string($address) . "%'"); 
//And we display the results 
while($result = mysql_fetch_array($data)) 
{ 
echo $result['fname']; 
echo "<br>"; 
echo $result['lname']; 
echo "<br>"; 
echo $result['info']; 
echo "<br>"; 
echo $result['zip']; 
echo "<br>"; 
echo $result['state']; 
echo "<br>"; 
echo $result['email']; 
echo "<br>"; 
    echo $result['address']; 
echo "<br>"; 
} 
//This counts the number or results - and if there wasn't any it gives them a little message explaining that 
$anymatches=mysql_num_rows($data); 
if ($anymatches == 0) 
{ 
echo "Sorry, but we can not find an entry to match your query<br><br>"; 
} 
//And we remind them what they searched for 
echo "<b>Searched For: 
     </b> " .$find; 
} 
?> 
+0

훨씬 안전합니다! – Perry

+0

http://stackoverflow.com/questions/60174/how-to-prevent-sql-injection-in-php/12817590#12817590 –

+0

mysql을 잊어 버리거나 PDO 또는 mysqli를 사용하십시오. 준비된 명령문도 도움이 될 것입니다. –

답변

-2

그냥 // We preform a bit of filtering 블록을 제거하십시오.
나머지는 꽤 괜찮습니다.

그러나 검색어로는 거의 아무것도 찾을 수 없습니다.
입력 한 값이 모두 들어있는 한 줄을 찾으려고합니까?
나는 그들이 사용되지 않습니다, 당신은 더 이상 MySQL의 기능을 사용하지 마십시오 OR하지 AND 당신의 CRITERIAS

-1

을 의미하는 느낌이 듭니다.

대신 mysqli 또는 pdo 클래스를 사용하십시오.

더 자세히 들어, 코드는 아주 못생긴, 이것 좀 봐 그 옆에

How can I prevent SQL injection in PHP?

을 게시 유래 걸릴. 보기에서 로그를 분리하십시오.

-1

PHP에서도 데이터베이스에 삽입하기 전에 데이터를 삭제할 수 있습니다. 이 코드를 확인하십시오.

<?php 
$email = "[email protected]"; //Note the .com missing 
if(filter_var($email, FILTER_SANITIZE_EMAIL)){ 
    echo $email.'<br>'; 
    var_dump(filter_var($email, FILTER_SANITIZE_EMAIL)); 
}else{ 
    var_dump(filter_var($email, FILTER_SANITIZE_EMAIL));  
} 
?> 

지원 링크 : 준비된 문에 php sanitize

대신 MySQL을 사용 PDO 또는 mysqli의
-1
<Paranoia> 

1. No using of old mysql functions (told already) 
2. intval() on zip-field 
3. regexp on others 

</Paranoia>