Is there some way to detect if a string has been base64_encoded() in PHP?
We\'re converting some storage from plain text to base64 and part of it lives in a cookie
We can combine three things into one function to check if given string is a valid base 64 encoded or not.
function validBase64($string)
{
$decoded = base64_decode($string, true);
// Check if there is no invalid character in string
if (!preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $string)) return false;
// Decode the string in strict mode and send the response
if (!$decoded) return false;
// Encode and compare it to original one
if (base64_encode($decoded) != $string) return false;
return true;
}