2016-05-18 2 views
0

私は、MySQLデータベースを使用して検索機能のために次のコードを持っています。PHPの検索フォームセキュリティの改善とその他の推奨

私の質問は次のとおりです。

1)検索結果で検索した単語を強調表示するにはどうすればいいですか(I がスクリーンショットに表示されています)。

2)art_content私のarticlesテーブルのフィールドには、全記事 が含まれています。検索結果の に記事の最初の数文だけを表示するにはどうすればよいですか?

3)SQLインジェクションなどの攻撃からこれを保護したい。 I が使用されている。strip_tags,mysqli_real_escape_string,trim。 これらの機能で十分ですか?提案はありますか?

screenshot

ここに私のコードです。

<html> 
    <head> 
    </head> 
    <body> 
     <form name="search" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 
      Search for: <input type="text" name="searchtext"/> 
        <input type="submit" name="ok" value="Search"/> 
     </form> 
    </body> 
</html> 

<?php 
    /* PHP simple search engine */ 
    /* 
     CREATE TABLE articles(
      art_id INT(4) NOT NULL PRIMARY KEY AUTO_INCREMENT, 
      art_title VARCHAR(200) NOT NULL, 
      art_date DATE NOT NULL, 
      art_content TEXT NOT NULL, 
      art_url VARCHAR(200) NOT NULL 
     ); 

     INSERT INTO articles 
     VALUES(1, 'Pirith Chanting in Galle', '2016-01-07', 'More than 200 students and Dhamma school teachers attented the ceremony.', 'galle_pirith.html'), 
     (2, 'Foods Distribution in Polonnaruwa', '2016-01-21', 'Vitims of flood received relief from foods distributed. President also participated', 'polo_foods.html'), 
     (3, 'Donations for Dhamma Schools', '2016-02-11', 'Financial support was given to Dhamma schools in remote areas in Galle district', 'dhamma_donation.html'); 
    */ 
if(isset($_POST['ok'])){ 
    $input = $_POST['searchtext']; 

    if($input==""){ 
     echo "<h1>Please enter a search term!</h1>"; 
     exit; 
    } 

    $con = @mysqli_connect("localhost:3306", "root", "[email protected]", "DogSport") or die ("Couldn't connect to server"); 

    //filtering input for XSS and SQL injection 
    $input = strip_tags($input); 
    $input = mysqli_real_escape_string($con, $input); 
    $input = trim($input); 

    $query = "SELECT * FROM articles WHERE art_title LIKE '%$input%' OR art_content LIKE '%$input%'"; 

    $result = mysqli_query ($con, $query) or die ("Couldn't execute SELECT query: ". mysqli_error($con)); 

    $nrows = mysqli_num_rows($result); 

    echo "<h3>". $nrows. " results found!</h3>"; 
    echo "<h4>You searched for: ". $input. "</h4>"; 

    while ($row = mysqli_fetch_array($result)){ 
      extract($row); 

      echo "<br><b> $art_title </b>"; 
      echo " - "; 
      echo "<b> $art_date </b><br>"; 
      echo "$art_content<br>"; 
      echo "<a href=$art_url>$art_url</a><br>"; 
    } 
} 
?> 

答えて

0

1)検索用語を強調表示するには、str_replace()関数を使用します。

echo str_replace($input, '<em>'.$input.'</em>', $variable_name); 

あなたは

2を加えはTO)

if (strlen($art_content) > [desired length of text]) 
{ 
    echo substr($art_content, 0, [desired length of text]) . '...'; 
} 
else 
{ 
    echo $art_content; 
} 

3テキストのある部分のみを抽出するためのsubstr()関数を使用)、CSSを使用してテキストを強調するために、例えば、のようなタグを使用することができSQLインジェクションからの保護PDO準備文 http://code.tutsplus.com/tutorials/why-you-should-be-using-phps-pdo-for-database-access--net-12059

関連する問題