2013-07-14 2 views
0

현재 사용 중입니다.mysql을 PDO에 연결 (xss 및 보안 삽입)

<html> 
<head> 
</head> 
<body> 
<form name="frmSearch" method="get" action="<?=$_SERVER['SCRIPT_NAME'];?>"> 
    <table width="599" border="1"> 
    <tr> 
     <th>Keyword 
     <input name="txtKeyword" type="text" id="txtKeyword" value="<?=$_GET["txtKeyword"];?>"> 
     <input type="submit" value="Search"></th> 
    </tr> 
    </table> 
</form> 
<? 
if($_GET["txtKeyword"] != "") 
    { 


    $objConnect = mysql_connect("XXXXX","XXXX","XXXX") or die(mysql_error()); 
    $objDB = mysql_select_db("XXXX"); 
    // Search By Name or Email 
    $strSQL = "SELECT * FROM blogs WHERE (title LIKE '%".$_GET["txtKeyword"]."%' or metadescription LIKE '%".$_GET["txtKeyword"]."%')"; 
    $objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]"); 
    $Num_Rows = mysql_num_rows($objQuery); 


    $Per_Page = 2; // Per Page 

    $Page = $_GET["Page"]; 
    if(!$_GET["Page"]) 
    { 
     $Page=1; 
    } 

    $Prev_Page = $Page-1; 
    $Next_Page = $Page+1; 

    $Page_Start = (($Per_Page*$Page)-$Per_Page); 
    if($Num_Rows<=$Per_Page) 
    { 
     $Num_Pages =1; 
    } 
    else if(($Num_Rows % $Per_Page)==0) 
    { 
     $Num_Pages =($Num_Rows/$Per_Page) ; 
    } 
    else 
    { 
     $Num_Pages =($Num_Rows/$Per_Page)+1; 
     $Num_Pages = (int)$Num_Pages; 
    } 


    $strSQL .=" order by id ASC LIMIT $Page_Start , $Per_Page"; 
    $objQuery = mysql_query($strSQL); 

    ?> 
<table width="600" border="1"> 
    <tr> 
    <th width="91"> <div align="center">CustomerID </div></th> 
    <th width="98"> <div align="center">Name </div></th> 
    <th width="198"> <div align="center">Email </div></th> 
    </tr> 
    <? 
    while($objResult = mysql_fetch_array($objQuery)) 
    { 
    ?> 
    <tr> 
    <td><div align="center"> 
     <?=$objResult["id"];?> 
     </div></td> 
    <td><?=$objResult["title"];?></td> 
    <td><?=$objResult["metadescription"];?></td> 
    </tr> 
    <? 
    } 
    ?> 
</table> 
<br> 
Total 
<?= $Num_Rows;?> 
Record : 
<?=$Num_Pages;?> 
Page : 
<? 
    if($Prev_Page) 
    { 
     echo " <a href='$_SERVER[SCRIPT_NAME]?Page=$Prev_Page&txtKeyword=$_GET[txtKeyword]'><< Back</a> "; 
    } 

    for($i=1; $i<=$Num_Pages; $i++){ 
     if($i != $Page) 
     { 
      echo "[ <a href='$_SERVER[SCRIPT_NAME]?Page=$i&txtKeyword=$_GET[txtKeyword]'>$i</a> ]"; 
     } 
     else 
     { 
      echo "<b> $i </b>"; 
     } 
    } 
    if($Page!=$Num_Pages) 
    { 
     echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=$Next_Page&txtKeyword=$_GET[txtKeyword]'>Next>></a> "; 
    } 

    mysql_close($objConnect); 

    } 
?> 
</body> 
</html> 

이 잘 작동하지만 난 그게 더 안전 기본적으로 내가 100 % 안전 할 필요는 등의 주입 및 XSS 공격을 견딜 수 있도록하고 싶습니다.

나는 그것을 PDO로 바꾸려고하고 있는데, 나는 지금까지 다음과 같은 것을 가지고있다; 내가 올 바르고 DB와의 두 개의 분리 된 상호 작용으로는 (? 내가 생각)

문제가 주사로부터 안전해야한다고 생각

$stmt = $pdo->prepare('SELECT * FROM blogs WHERE title LIKE = ?'); 
$stmt->execute($_POST['txtKeyword']); 

; 1) 나는 이것과 다른 만드는 데 필요한 무엇 2)을 구현하는 방법을 잘 모르겠어요 있는지 검색

내가 어떤 도움을 주셔서 감사합니다 정말 것

+0

"100 % 안전"과 같은 것이 없습니다. – eggyal

+0

안녕하세요. 좋은 pdo 튜토리얼 http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers.Also http://stackoverflow.com/questions/134099/are-pdo-prepared-statements-sufficient-to-prevent-sql을 확인하십시오. -jection – Arunu

+1

1. 키워드에 백분율 표시를 추가해야합니다. 2. "PDO 연결"은 "xss 공격 등과 관련이 없습니다" 그래서, 그것은 다른 질문입니다. –

답변

-1

먼저 execute() 구조는 배열을 기대 안전 그래서, 당신은 당신이 XXS, 공격을 방지하려면

$stmt = $pdo->prepare('SELECT * FROM blogs WHERE title LIKE = ?'); 
$stmt->execute(array($_POST['txtKeyword'])); 

그리고 두 번째는, 당신은 단지 htmlspecialchars()을 사용하여 데이터베이스에서 모든 것을 출력하는 동안, 당신에게 데이터를 살균하는 htmlspecialchars() 기능을 사용한다는 것을 확인해야하며 변환합니다 모든 태그, 그래서 t 너 모자가 안전 해.

+0

모든 귀. DV는 무엇에 관한 것인가? –

+0

첫 번째 라인이 완전하지 않습니다 [Manual] {http://php.net/manual/en/pdostatement.execute.php}. PDO 태그를주의하십시오. 내 DV가 아닙니다. –

+0

그것이 어디에서 읽지 못하면 필요하지 않습니다. –