Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
PHP – Detect HTTP input character encoding with mb_http_input()
The mb_http_input() function in PHP is used to detect the HTTP input character encoding from various sources like GET, POST, COOKIE data, and strings. This function is particularly useful when working with different character encodings in web applications and is supported in PHP 5.4 or higher.
Syntax
array|string mb_http_input(string $type = null)
Parameters
mb_http_input() accepts a single optional parameter −
-
$type − Specifies the input type to check for character encoding:
G − GET variables
P − POST variables
C − COOKIE variables
S − String variables
L − List of all types
I − Returns array of all detected encodings
If $type is omitted, it returns the character encoding of the last processed input.
Return Value
Returns the character encoding name as a string for specific types, or an array of character encoding names when type is "I" or "L". Returns false if no HTTP input has been processed for the specified type.
Example 1: Getting All Input Encodings
<?php
// Get all detected input character encodings
$encodings = mb_http_input("I");
var_dump($encodings);
?>
array(1) {
[0]=>
string(5) "UTF-8"
}
Example 2: Checking Specific Input Types
<?php
// Check GET input encoding
$get_encoding = mb_http_input("G");
echo "GET encoding: " . ($get_encoding ?: "None detected") . "<br>";
// Check POST input encoding
$post_encoding = mb_http_input("P");
echo "POST encoding: " . ($post_encoding ?: "None detected") . "<br>";
// Get last processed input encoding
$last_encoding = mb_http_input();
echo "Last processed: " . ($last_encoding ?: "None") . "<br>";
?>
GET encoding: None detected POST encoding: None detected Last processed: None
Key Points
The function only works when HTTP input has actually been processed
In CLI mode, it typically returns false or empty results
Most useful in web contexts where form data or URL parameters contain multi-byte characters
Requires the mbstring extension to be enabled
Conclusion
The mb_http_input() function helps detect character encodings from HTTP input data, making it valuable for handling international content in web applications. Use it to ensure proper character encoding detection when processing user input from different sources.
