0

これは私のphpコードです:

//Code for fetching content (view-me.php)
<?php
include('connect.php');
$head_title=$_GET['title'];
//echo $head_title;
$head_title=$mysqli->real_escape_string($head_title);
//echo $head_title;
$query="select title,content,pub_date from news where title='$head_title'";
$result=$mysqli->query($query);
if($result){
    while(list($title,$content,$date)=$result->fetch_row())
    {
        echo '<div id="post_my_head">';
        echo "<h1>".$title."</h1></div><br />";
        echo '<div id="posted_date_time">'.$date.'</div>';
        echo '<div id="post_my_content"><p>'.$content.'</p></div>';

    }


    $mysqli->close();
}
else
    {
        echo "Query failed";
    }

?>

//Code for printing content:(news.php)
function print_content()
{
    include('connect.php');
    $query="select title,content,url,pub_date from news order by pub_date desc";
    $result=$mysqli->query($query);
    while(list($title,$content,$url,$date)=$result->fetch_row())
    {
        echo '<div id="post_my_head">';
        echo "<h1><a href='view-me.php?title=".addslashes($title)."' id='my_post_link'>".$title."</a></h1></div><br />";
        //$new_title=str_replace(" ","-",$title);
        //echo "localhost/".$new_title."/";
        echo '<div id="posted_date_time">Published:'.$date.'</div><br /><br />';
        echo '<div id="post_my_content"><p>'.$content.'</p></div>';
    }
    $mysqli->close();
}
print_content();

$head_title に一重引用符または二重引用符があると失敗します。news.php で addslashes を使用しようとしましたが、機能しません。

$head_title が "America's Freedom" のような場合、コードは失敗します。この場合、$title は "America" になり、残りの文字列は自動的に切り捨てられます。したがって、URL は "localhost/?title=America" のようになります。 「localhost?title=America's Freedom」。mysql で使用されるデータ型はテキストです。コードは、一重引用符または二重引用符を使用しないすべてのクエリで完璧に機能します。

4

3 に答える 3

1

URL の場合、値を URL エンコードします。そのエンコードされた URL を HTML に入れるために、HTML はそれを上にエスケープします。

printf('<h1><a href="view-me.php?title=%s" id="my_post_link">%s</a></h1>',
       htmlspecialchars(urlencode($title)),
       htmlspecialchars($title));

The Great Escapism (または、テキスト内のテキストを操作するために知っておくべきこと)を参照してください。

于 2012-10-03T09:58:09.427 に答える
1

これを修正するには、 urlencode()と urldecode を使用できます。

于 2012-10-03T09:56:22.883 に答える
0
<?php
$str = "Jane & 'Tarzan'";
echo htmlentities($str, ENT_COMPAT);
echo "<br />";
echo htmlentities($str, ENT_QUOTES);
echo "<br />";
echo htmlentities($str);
?>

htmlentities を試す

于 2012-10-03T09:55:52.387 に答える