【问题标题】:php urlencode and htmlspecialchars a variable inside started function within html <a href>php urlencode 和 htmlspecialchars 在 html <a href> 中启动函数内的变量
【发布时间】:2016-03-18 17:24:14
【问题描述】:

我有这个功能:

function after_char($char, $var) {
    $get = explode($char, $var);
    $text= $get[1];
    echo $text;
}

我有 html 链接:

<a href="http://<?php after_char('@', urlencode($post_email)); ?>" target="_blank">Potwierdź aktywację konta w <?php after_char('@', htmlspecialchars($post_email, ENT_QUOTES)); ?>.</a> 

两个启动函数中的变量应该如何编码? 我真的需要写第二个相同的功能吗?第一个用于 urlencode,第二个用于 htmlspecialchars 并在函数构建中而不是在启动函数中对其进行编码?

【问题讨论】:

    标签: php function xss urlencode htmlspecialchars


    【解决方案1】:

    您已经颠倒了操作顺序。最好先在函数中准备数据,将其返回,然后针对特定上下文对其进行编码并回显。

    使用rawurlencode() 对将进入href= 的URL 进行编码。
    使用 htmlspecialchars 对 HTML 上下文中显示的任何其他文本进行编码。

    例子:

    <?php
    
    function after_char($char, $var): string {
        $get  = explode($char, $var, 2);
        return $get[1]; // returns the part after the $char
    }
    
    $result_of_after_char = after_char('@', $post_email);
    ?>
    <a href="http://<?= urlencode($result_of_after_char); ?>" target="_blank"
        >Potwierdź aktywację konta w <?= htmlspecialchars($result_of_after_char , ENT_QUOTES); ?>.</a>
    

    另一方面,函数after_char 并没有比explode() 做更多的事情。您可以完全摆脱该功能:

    $result_of_after_char = explode('@', $post_email, 2)[1];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多