2016-05-18 13 views
0

Ich habe folgenden Code für die Suchfunktion mit MySQL-Datenbank;PHP Suchformular Sicherheitsverbesserungen und andere Empfehlungen

Meine Fragen sind;

1) Wie kann ich das gesuchte Wort in den Suchergebnissen markieren (wie I im Screenshot gezeigt haben).

2) art_content Feld meiner articles Tabelle enthält ganzen Artikel Inhalt. Wie kann ich nur die ersten paar Sätze der Artikel in den Suchergebnissen anzeigen?

3) Ich möchte dies vor SQL-Injektion und anderen Angriffen schützen. I haben verwendet strip_tags, mysqli_real_escape_string, trim. Sind diese Funktionen ausreichend? Gibt es irgendwelche Vorschläge?

screenshot

Hier ist mein Code;

<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>"; 
    } 
} 
?> 

Antwort

0

1) Um Suchbegriffe zu markieren, verwenden Sie die Funktion str_replace().

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

können Sie Tags verwenden, wie, Text zu markieren mit CSS

hinzugefügt

2) Verwenden Sie substr() Funktion nur bestimmten Teil des Textes zu extrahieren

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

3), um Schutz vor SQL-Injection-Verwendung PDO vorbereitete Anweisung http://code.tutsplus.com/tutorials/why-you-should-be-using-phps-pdo-for-database-access--net-12059