首页 > 解决方案 > htmlentities 关于 PHP 的最佳实践问题

问题描述

您应该在 htmlentities() 内部还是在 PHP 外部有 urlencode() ?

关于 URL 中的查询字符串。第一部分可以在 htmlentities() 内还是只有最后一部分?也许两者兼而有之?

如果 URL 如下所示: Url: site.com/?url=go.com

第一部分:url=

上一部分:go.com

直接将 URL 放在 htmlentities() 中怎么样?还是应该将 URL 放在一个变量中,然后将该变量放在 htmlentities() 中?

请向我们展示一些你自己如何做的例子。

干杯。

我编写了 4 种不同的工作方式,但怀疑并非所有方式都有效。需要您从下面指出不正确或不好的做法示例。

例 1

<?php
//EXAMPLE 1: 
//urlencode() outside htmlentities().
//Url in a Variable. Then that Var is inside htmlentities().
//htmlentities() exclude URL query strings.

$selfpage = $_SERVER['PHP_SELF'];
echo 'Page: ';
if($page>$total_pages)
{
    echo '<a href="' .htmlentities("$selfpage") .'?tbl=' .urlencode("$tbl") .'&col=' .urlencode("$col") .'&search=' .urlencode("$search") .'&page=' .$total_pages .'"><font size="8"><b>' .$total_pages .'</b></a></font>';
}
else
{
    while($i<=$total_pages)
    {
        if($i==$page)
        {
            echo '<a href="' .htmlentities($selfpage) .'?tbl=' .urlencode($tbl) .'&col=' .urlencode($col) .'&search=' .urlencode($search) .'&page=' .$i .'"><font size="8"><b>' .$i .'</b></a></font>';
        }
        else
        {
            echo '<a href="' .htmlentities($selfpage) .'?tbl=' .urlencode($tbl) .'&col=' .urlencode($col) .'&search=' .urlencode($search) .'&page=' .$i .'"><font size="4">' .$i .'</a></font>';
        }
        $i++;
    }
}
echo '<br>';
echo '<br>';
?>

例 2

<?php
//EXAMPLE 2:
//urlencode() outside htmlentities().
//Url directly inserted inside htmlentities().
//htmlentities() exclude URL query strings.

echo 'Page: ';
if($page>$total_pages)
{
    echo '<a href="' .htmlentities("http://www.ydomain.com/search/pagination.php") .'?tbl=' .urlencode("$tbl") .'&col=' .urlencode("$col") .'&search=' .urlencode("$search") .'&page=' .urlencode($total_pages) .'"><font size="8"><b>' .$total_pages .'</b></a></font>';
}
else
{
    $i = 1;
    while($i<=$total_pages)
    {
        if($i==$page)
        {
            echo '<a href="' .htmlentities("http://www.ydomain.com/search/pagination.php") .'?tbl=' .urlencode($tbl) .'&col=' .urlencode($col) .'&search=' .urlencode($search) .'&page=' .$i .'"><font size="8"><b>' .urlencode($i) .'</b></a></font>';
        }
        else
        {
            echo '<a href="' .htmlentities("http://www.ydomain.com/search/pagination.php") .'?tbl=' .urlencode($tbl) .'&col=' .urlencode($col) .'&search=' .urlencode($search) .'&page=' .$i .'"><font size="4">' .urlencode($i) .'</a></font>';
        }
        $i++;
    }
}
echo '<br>';
echo '<br>';
?>

例 3

<?php
//EXAMPLE 3:
//urlencode() inside htmlentities().
//Url in a Variable. Then that Var is inside htmlentities().
//htmlentities() include both query strings and non-query string parts of URL.

echo 'Page: ';
if($page>$total_pages)
{
    $final_page = 'http://www.ydomain.com/search/pagination.php' .'?tbl=' .urlencode("$tbl") .'&col=' .urlencode("$col") .'&search=' .urlencode("$search") .'&page=' .urlencode($total_pages);
    echo '<a href="' .htmlentities("$final_page") .'"><font size="8"><b>' .$total_pages .'</b></a></font>';
}
else
{
    $i = 1;
    while($i<=$total_pages)
    {
        $self_page = 'http://www.ydomain.com/search/pagination.php' .'?tbl=' .urlencode("$tbl") .'&col=' .urlencode("$col") .'&search=' .urlencode("$search") .'&page=' .urlencode($i);
        if($i==$page)
        {
            echo '<a href="' .htmlentities("$self_page") .'"><font size="8">' .$i .'</a></font>';
        }
        else
        {
            echo '<a href="' .htmlentities("$self_page") .'"><font size="4">' .$i .'</a></font>';
        }
        $i++;
    }
}
?>

例 4

<?php
//EXAMPLE 4:
//urlencode() inside htmlentities().
//htmlentities() include only query strings and non-query string parts of URL excluded from htmlentities().

$self_page = "http://www.ydomain.com/search/pagination.php";
echo 'Page: ';
if($page>$total_pages)
{
    $final_page_query_string = '?tbl=' .urlencode("$tbl") .'&col=' .urlencode("$col") .'&search=' .urlencode("$search") .'&page=' .urlencode("$total_pages");
    echo '<a href="' ."$self_page" .htmlentities("$final_page_query_string") .'"><font size="8"><b>' .$total_pages .'</b></a></font>';
}
else
{
    $i = 1;
    while($i<=$total_pages)
    {
        if($i==$page)
        {
            $query_string = '?tbl=' .urlencode("$tbl") .'&col=' .urlencode("$col") .'&search=' .urlencode("$search") .'&page=' .urlencode("$i");
            echo '<a href="' ."$self_page" .htmlentities("$query_string") .'"><font size="8"><b>' .$i .'</b></a></font>';
        }
        else
        {
            $query_string = '?tbl=' .urlencode("$tbl") .'&col=' .urlencode("$col") .'&search=' .urlencode("$search") .'&page=' .urlencode("$i");
            echo '<a href="' ."$self_page" .htmlentities("$query_string") .'"><font size="4">' .$i .'</a></font>';
        }
        $i++;
    }
}

?>

标签: phpurlhtml-entities

解决方案


推荐阅读