tags:

views:

358

answers:

1

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 that needs to be updated. I'd like to reset their cookie if the text has not yet been encoded, otherwise leave it alone.

+5  A: 

base64_decode() returns FALSE if the input is not valid base64 encoded data. You could also consider a regular expression for pre-validation, but base64_decode should be enough unless you have huge amounts of data to test. Naturally, any text may "look" like base64 encoded text, but the probability for random text fulfilling the requirements is quite low.

Matti Virkkunen
Thanks Matti! Looks like the strict flag needs to be set, so: $encoded = base64_decode('string to text', true);
Ian Silber