【问题标题】:Send php code via ajax not working通过ajax发送php代码不起作用
【发布时间】:2013-01-16 19:02:25
【问题描述】:

我正在尝试使用 jquery 发出 ajax 请求,但每次发送 php 函数时都会收到 403 Forbidden 错误。我需要的一个例子是这个 stackoverflow 注释输入,它也接受源代码。

我做了什么:

PHP

class Error {
  public function __construct() {
    // etc
  }

  public function comment($error_id, $content) {

    if(!$this->user->loged)
      return false;

    $error_id = $this->mysqli->real_escape_string($error_id);
    if(!is_numeric($error_id))
      return false;

    $this->mysqli->query("INSERT INTO comments 
                          VALUES (NULL, '" . $error_id . "', '" . $this->user->user_id . "', '" . $content . "', NULL)");
    return $this->mysqli->insert_id;

  }
}

$contentCommon 类中使用 escapeString 函数转义:

public function escapeString($text) {
  return htmlspecialchars($this->mysqli->real_escape_string($text));
}

我的 JS 是:

$('div.left-content').on('click','form.comment input[type=button]',function(){

        $but = $(this);

        if(!erori.user.loged) {

            showTooltip($but, 'Trebuie să fii autentificat pentru a putea comenta!');

            return false;
        }

        if($but.prev('textarea').val() == $but.prev('textarea').data("text") || $but.prev('textarea').val().trim() == '') {

            showTooltip($but, 'Completează câmpul de text pentru a putea trimite comentariul!');

            return false;
        }

        if($but.prev('textarea').hasClass('disabled'))
            return false;

        $but.attr('disabled', 'disabled');
        $but.prev('textarea').addClass('disabled');

        $.post(
            $but.parent().attr('action'),
            $but.parent().serialize(),
            function(data){
                if(data.content) {
                    $('<div class="comment"></div>').html(data.content).insertBefore($but.parent().parent());
                    if($but.prev('textarea').hasClass('disabled'))
                        $but.prev('textarea').val('').trigger('blur').animate({height: '20px'}, 300);
                    $but.prev('textarea').removeClass('disabled');
                }

                $but.removeAttr('disabled');

            },
            'json')
        .fail(function(xhr, textStatus, errorThrown) {
            if(errorThrown == 'Forbidden') {
                showTooltip($but, 'Comentariul nu a putut fi trimis!<br />Dacă vrei să trimiți sursă de cod folosește <strong>` cod sursă `</strong>!');

                $but.prev('textarea').removeClass('disabled');
                $but.removeAttr('disabled');

                return false;
            }
        })                                                         
    }); 

动作代码是:

$error = new Error($mysqli, $user);

$content = $common->escapeString($_POST['error_comment']);

$comment_id = $error->comment($_POST['error_id'], $content);

如何在将源代码发送到服务器之前对其进行转义,以免返回此 403 Forbidden 错误?

我想说的是,如果我想评论例如:This is my comment with &lt;?php $sql = mysql_query(); ?&gt; 服务器正在抛出 403 错误代码!

【问题讨论】:

  • 如果你在没有 php-code 的情况下写评论,一切都好吗?
  • 是的.. 一切正常.. 即使我编写了 js、css 或一些 php 代码.. 一切正常.. 但是当尝试发送一些 php 函数时它返回 403 错误

标签: php jquery ajax http-status-code-403


【解决方案1】:

您不能在发送之前真正过滤您的代码,因为用户可以关闭 javascript 并直接向您发送代码,跳过任何客户端过滤。

还有 403 错误 - 这是一个“权限”错误,因此您需要修复 php 文件的标题或权限。

【讨论】:

  • 对..观点不错..那么服务器端的解决方案是什么?因为不评估代码...
  • 为什么不扩展 "escapeString($text)" 并放置您需要的其他过滤器?
  • 那些过滤器是什么? :) 这就是我需要的 .. 但我不知道那些过滤器
  • 实际上,你可以为
猜你喜欢
  • 2012-02-17
  • 1970-01-01
  • 1970-01-01
  • 2012-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-28
相关资源
最近更新 更多