$_POST empty on utf-8 characters

2020-01-29 09:24发布

I'm working on a multilingual site with CodeIgniter. There is a form that posts data to controller, but $_POST is empty when I start to use Turkish characters like öçüÜĞ etc.

I set the charset to:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

Form:

<form action="translations/save" method="post" accept-charset="utf-8">
    <textarea rows="6" cols="60" id="editor_tr" name="editor_tr">Türkçe</textarea>
</form>

$_POST and $this->input->post('editor_tr') returns empty, but I can see the raw post with file_get_contents("php://input").

This one works OK in a normal PHP test, but doesn't work with CodeIgniter. Perhaps my .htaccess file is causing the issue, but dunno.

Any help is much appreciated.

UPDATE: Here is the output for var_dump as requested.

var_dump($_POST) - Without Turkish chars

array(3) { ["id"]=> string(12) "news8titleID" ["editor_tr"]=> string(13) "turkish value" ["editor_en"]=> string(13) "english value" }

var_dump($_POST) - With Turkish chars (The input was: Türkçe karakter, but it doesn't show up in the $_POST)

array(3) { ["id"]=> string(12) "news8titleID" ["editor_tr"]=> string(0) "" ["editor_en"]=> string(13) "english value" }

UPDATE 2: When debugging, I have found out that system.core.Input class cleans the input data on _clean_input_data function.

// Clean UTF-8 if supported
if (UTF8_ENABLED === TRUE)
{
    $str = $this->uni->clean_string($str);
}

So, before the $_POST has reached to my controller, the editor_tr value is already cleaned by system.core.Utf8 class in this function:

function clean_string($str)
{
    if ($this->_is_ascii($str) === FALSE)
    {
        $str = @iconv('UTF-8', 'UTF-8//IGNORE', $str);
    }

    return $str;
}

4条回答
We Are One
2楼-- · 2020-01-29 09:29

Since the comments are piling up and will probably get overlooked, I am going to post some more suggestions.

Readers please keep in mind this is dealing with $_POST data values and not display of data on a web page.

This user seems to have a similar problem:

Codeigniter seems to break $_POST of '£' character (Pound)

There is a report here on Bitbucket of similar:

https://bitbucket.org/ellislab/codeigniter-reactor/issue/214/problem-when-inserting-special-characters: Removed link: EllisLabs has closed this repo to the public

Maybe adding this to your index.php will help (probably not):

ini_set('default_charset', 'UTF-8');

Double check and make sure you aren't running any validation or prepping rules on the field. Things like url_title() will strip those characters.

查看更多
姐就是有狂的资本
3楼-- · 2020-01-29 09:29

This isnt an answer, but you might want to take a look and see how everything is coming in.


foreach($_POST as $key => $val)
{
     $post_data[$key] => $val;
}
print_r($post_data);

then try with CI's post


foreach($_POST as $key => $val)
{
     $post_data[$key] => $this->input->post($key);
}

print_r($post_data);
查看更多
Summer. ? 凉城
4楼-- · 2020-01-29 09:47

Make sure the form tag has accept-charset:

<form method="post" action="" accept-charset="utf-8">

Then in the controller use utf8_decode() when fetching the posted values:

$value = utf8_decode($this->input->post('field_name'));
查看更多
太酷不给撩
5楼-- · 2020-01-29 09:50

If you don't want to use a previous PHP version in your MAMP instalation you can use:

$_REQUEST

To get the data instead of $_POST

$_REQUEST is an associative array that by default contains the contents of $_GET, $_POST and $_COOKIE.

More info: http://php.net/manual/en/reserved.variables.request.php

And that returns all the data that for some reason $_POSTis breaking !

查看更多
登录 后发表回答