0

我正在尝试使用 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;

  }
}

where使用类内的函数$content进行转义:escapeStringCommon

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 <?php $sql = mysql_query(); ?>服务器正在抛出 403 错误代码!

4

1 回答 1

2

You cannot really filter your code before sending, because user can just turn off javascript and send code to you directly, skipping any clientside filtering.

And 403 error - it's a "rights" error, so you need to fix headers or right for php-file.

于 2013-02-01T12:57:34.380 回答