bad formatted JSON giving error
-
Hello,
I have the same fatal error as reported on this thread:
PHP Fatal error:? Uncaught TypeError: array_map(): Argument #2 ($array) must be of type array, null given in …/public_html/wp-content/plugins/mobile-login-woocommerce/includes/class-xoo-ml-geolocation.php:25
The error is caused by a bad formatted JSON.
Even though you use stripslashes, the output of stripslashes still has slashes.
You can easily solve it by replacing your line of code:return array_map( 'sanitize_text_field', json_decode( stripslashes( $_COOKIE['xoo_ml_user_ip_data'] ), true ) );
with this one:
return array_map( 'sanitize_text_field', json_decode( stripslashes( stripslashes( $_COOKIE['xoo_ml_user_ip_data'] ) ), true ) );
However, for me, it would be better if you first define a variable e.g. $arr = json_decode( stripslashes( stripslashes( sanitize_text_field( $_COOKIE[‘xoo_ml_user_ip_data’] ) ) ), true ), and then you check if $arr is really a non-empty array before applying array_map.
I would also use sanitize_text_field because you don’t know what is inside $_COOKIE[‘xoo_ml_user_ip_data’], and stripslashes is not enough to sanitize the input.I hope it helps to solve this issue. In some situations your code triggers a fatal error and the website can’t be seen at all.
Have a great day!
Jose
- The topic ‘bad formatted JSON giving error’ is closed to new replies.