Escaping output safely for both html and input fields

后端 未结 3 1813
名媛妹妹
名媛妹妹 2020-12-19 11:32

In my web app, users can input text data. This data can be shown to other users, and the original author can also go back and edit their data. I\'m looking for the correct w

3条回答
  •  离开以前
    2020-12-19 12:03

    I'm sorry but I cannot reproduce the behaviour you describe. I've always used htmlspecialchars() (which does essentially the same task as htmlentities()) and it's never lead to any sort of double-encoding. The page source shows déjà vu in both places (of course! that's the point!) but the rendered page shows the appropriate values and that's what sent back to the server.

    Can you post a full self-contained code snippet that exhibits such behaviour?

    Update: some testing code:

    
    
    
    
    
    
    
     ¿foo?';
    
    if( !isset($_GET['foo']) ){
        $_GET['foo'] = $default_value;
    }
    
    ?>
    
    

    Answer to updated question

    The htmlentities() function, as its name suggests, is used when generating HTML output. That's why it's of little use in your second example: JavaScript is not HTML. It's a language of its own with its own syntax.

    Now, the problem you want to fix is how to generate output that follows these two rules:

    1. It's a valid string in JavaScript.
    2. It can be embedded safely in an HTML document.

    The closest PHP function for #1 I'm aware of is json_encode(). Since JSON syntax is a subset of JavaScript, if you feed it with a PHP string it will output a JavaScript string.

    As about #2, once the browser enters a JavaScript block it expects a tag to leave it. The json_encode() function takes care of this and escapes it properly (<\/script>).

    My revised test code:

     ¿foo?';
    
    if( !isset($_GET['foo']) ){
        $_GET['foo'] = $default_value;
    }
    
    ?>
    
    
    
    
    
    
    
    
    
    
    

    Note: utf8_encode() converts from ISO-8859-1 to UTF-8 and it isn't required if your data is already in UTF-8 (recommended).

提交回复
热议问题