How to handle user input of invalid UTF-8 characters?

后端 未结 9 2024
小鲜肉
小鲜肉 2020-11-29 17:26

I\'m looking for general a strategy/advice on how to handle invalid UTF-8 input from users.

Even though my webapp uses UTF-8, somehow some users enter invalid chara

9条回答
  •  再見小時候
    2020-11-29 18:29

    Try doing what Rails does to force all browsers always to post UTF-8 data:

    See railssnowman.info or the initial patch for an explanation.

    1. To have the browser sends form-submission data in the UTF-8 encoding, just render the page with a Content-Type header of "text/html; charset=utf-8" (or use a meta http-equiv tag).
    2. To have the browser sends form-submission data in the UTF-8 encoding, even if the user fiddles with the page encoding (browsers let users do that), use accept-charset="UTF-8" in the form.
    3. To have the browser sends form-submission data in the UTF-8 encoding, even if the user fiddles with the page encoding (browsers let users do that), and even if the browser is IE and the user switched the page encoding to Korean and entered Korean characters in the form fields, add a hidden input to the form with a value such as which can only be from the Unicode charset (and, in this example, not the Korean charset).

提交回复
热议问题