Why is PHP mb_check_encoding producing different results for the same string? [closed]

23 hours ago 2
ARTICLE AD BOX

When I try out the PHP mb_check_encoding function with different strings in a simple PHP file it successfully produces the results I expect. (See the following code:)

On checking the string "\x65\x92" it returns false which is what I expect (as it is not utf-8).

<?php $myString = "\x65\x92"; $isValidEncoding = mb_check_encoding($myString, 'UTF-8'); if(!$isValidEncoding){ //This code does successfully execute //since the string is not utf-8 encoding. echo "Invalid Encoding!"; } ?>

However when I use the function within a PHP API that I have created it produces different results and I'm hoping you can help me know the reason why.

Basic description of my application:

I have an Angular (version 20) application which makes requests to my PHP API. (I have PHP version 7.4.9 installed on my Wamp Server).

I currently have a contact form in the Angular application and on Submitting the form (with "post" method) I am sending the data (in JSON format) to the PHP API.

I've set the 'Content-Type' header in the Angular application to be 'application/json' as follows:

const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json' }) };

I retrieve the data in the PHP API in the following way:

//We need to get the data the following way because we have the //Content-Type header set to application/json, //so $_POST will no longer be populated $rawData = file_get_contents('php://input'); $jsonData = json_decode($rawData);

After receiving this data I check that it is utf-8 encoding (in the same way I did in the simple PHP file mentioned above). (Note: The reason I want to do this in my application is in order to prevent "Invalid Encoding Attacks").

$isValidEncoding = mb_check_encoding($subject, 'UTF-8');

Now In order to test this I've entered the string "\x65\x92" (without the quotes) to the contact form in the "subject" field.

And on the server side I expected the result of mb_check_encoding($subject, 'UTF-8') to be false (as the input value is not utf-8) however it is returning true.

I was wondering what is the reason for this unexpected result? Thanks for your help :)

Read Entire Article