Current Location: Home> Latest Articles> How to use mb_get_info with mb_http_input to get encoding in HTTP request

How to use mb_get_info with mb_http_input to get encoding in HTTP request

gitbox 2025-05-29

In PHP development, when dealing with multibyte strings, we often use mbstring extensions to ensure the correctness of character encoding, especially when processing multilingual websites or processing data from different clients. To correctly handle character encoding in requests, mb_http_input() and mb_get_info() are very practical tools.

This article will explain in detail how to obtain character encodings in HTTP requests through these two functions and explain their uses and differences.

1. mb_http_input — Get the character encoding of HTTP input

The mb_http_input() function can be used to detect the character encoding of HTTP input. The input types it can detect include:

  • 'G' :GET data

  • 'P' : POST data

  • 'C' : COOKIE data

  • 'S' : string input (all)

  • 'I' : Automatic detection (Priority: POST > GET > COOKIE)

Sample code:

 <?php
// Test POST Character encoding of data
$post_encoding = mb_http_input('P');

if ($post_encoding) {
    echo "POST The requested character encoding is: {$post_encoding}\n";
} else {
    echo "未能Test到 POST Requested encoding\n";
}
?>

If you submit data in a form, for example:

 <form method="post" action="https://gitbox.net/submit.php">
  <input type="text" name="username">
  <input type="submit" value="submit">
</form>

The above PHP script can detect the encoding used by your submitted POST data (such as UTF-8 or EUC-JP).

2. mb_get_info — Get mbstring configuration information

mb_get_info() is a multi-functional diagnostic function that can return the current configuration information of mbstring . If no parameters are passed during the call, all information will be returned; you can also pass in a specific project name to query a certain setting, such as "http_input" .

Sample code:

 <?php
// Get all mbstring information
$mb_info = mb_get_info();
echo "Current HTTP The input code is set to: " . $mb_info['http_input'] . "\n";

// Or just get http_input part
$http_input = mb_get_info("http_input");
echo "HTTP Enter the encoding settings: {$http_input}\n";
?>

Note: mb_get_info("http_input") returns the default input encoding settings of mbstring (such as auto , UTF-8 , etc.), rather than the actual request data encoding. Therefore, mb_http_input() is the first choice for checking "what encoding is used in the actual request".

3. The combination of two functions

If you want to ensure that the server can correctly receive and process request data from the client, the following is a practical application method:

 <?php
// Set default character encoding
mb_internal_encoding("UTF-8");
mb_http_input("auto");

// Get POST The actual character encoding requested
$actual_encoding = mb_http_input('P');

// Get mbstring Current HTTP Enter settings
$configured_encoding = mb_get_info("http_input");

echo "The actual request code is: {$actual_encoding}\n";
echo "mbstring Configured HTTP Enter settings为: {$configured_encoding}\n";
?>

This code helps developers troubleshoot garbled characters: whether the encoding sent by the client does not match the server configuration, or the mbstring setting fails to override the default behavior.

4. Summary

  • Use mb_http_input() to get the character encoding actually used in the request.

  • Use mb_get_info() to get the current configuration of mbstring , including the default HTTP input encoding.

  • The combination of the two allows a more comprehensive understanding and debugging of coding issues, ensuring that the application can handle user input correctly.

It is recommended that mbstring is always enabled and correctly configured when processing multilingual websites or international form data, and regularly check whether the server behavior matches the client data through the above methods.

Are you debugging the garbled character code problem, or developing applications involving different locales?