php - 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++;
}
}
?>
解决方案
推荐阅读
- sharepoint - 如何在 Sharepoint 中创建可自定义的列
- oracle - 调用立即执行时错误数字溢出oracle
- java - 使用 Java 从 XML 文件中读取信息
- regex - 用于匹配两边没有空格的字符的正则表达式
- amazon-web-services - 如何从我的颤振应用程序将数据发布到 AWS API 网关?
- java - Android Studio:使用firebase数据库查询用户数据需要时间加载
- android - flutter_blue 设备连接永远挂起
- sql - 从此文件加载数据。(加载为 EXCEL 或 CSV)
- typescript - 如何将带有表单数据的 ant design(typescript) 中的 info.fileList[0].originFileObj 发送到服务器?
- mysql - 无法在远程 MySQL 服务器上创建存储过程