サポート » 使い方全般 » エスケープに使う関数

  • 解決済 coooookiiie

    (@coooookiiie)


    AJAXで受け取る値について、
    esc_html
    fiter_input
    はどちらをどのように使ったらいいでしょうか

    例えば以下nameという文字列を検証したいなら、どちらの方法がいいですか?

    add_action( 'wp_ajax_my_ajax_update_name', 'my_ajax_update_name' );
    function my_ajax_update_name() {
    	ini_set('display_errors',1);
    
    	$action = 'my_ajax_update_name';
    	$keyword = $action . 'himitsu';
    	if( ! check_ajax_referer($keyword, 'nonce', false) ) die('nonce does not match');
    	if( ! is_user_logged_in() ) die('Please login');
    
    	// 以下どちらを使えばいいのかわかりません
    	$name = $_POST['name'] ? esc_html( $_POST['name'] ) : '';
    	$name = filter_input(INPUT_POST, 'name');
2件の返信を表示中 - 1 - 2件目 (全2件中)
  • enomoto celtislab

    (@enomoto-celtislab)

    最近プラグインを登録した時に次のように指摘されました

    Don’t use esc_ functions to sanitize

    When sanitizing data, it’s important to use sanitization functions, not escape functions. The two work together, but are not interchangeable.

    Functions like esc_attr() do NOT sanitize anything, and should never be used for that purpose.

    The sole exception to this is URLs, which can use esc_url() or esc_url_raw() when being saved.

    Please review this document for help finding the most appropriate sanitization functions: https://developer.wordpress.org/plugins/security/securing-input/

    esc_html は、推奨されません
    リンクにあるサニタイズ関数から適切なものを使えば良いですが、
    入力されてくる文字列がいくつか決まっているなら in_array() で判定するのがいいかもしれません

    トピック投稿者 coooookiiie

    (@coooookiiie)

    遅くなってしまって申し訳ございません。
    それは困りますね。情報どうもありがとうございました。

2件の返信を表示中 - 1 - 2件目 (全2件中)
  • トピック「エスケープに使う関数」には新たに返信することはできません。