检测base64编码在PHP? [英] Detect base64 encoding in PHP?

查看:397
本文介绍了检测base64编码在PHP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一些方法,如果一个字符串已经在PHP被base64_en codeD()来检测?

Is there some way to detect if a string has been base64_encoded() in PHP?

我们正在从纯文本转换成一些存储为base64和部分它生活在一个需要更新的cookie。我想重置自己的cookie如果文本目前尚未连接$ C $光盘,否则不要管它。

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.

推荐答案

base64_de code()返回FALSE。你也可以考虑为pre-验证一个普通的前pression,但base64_de code应该是足够的,除非你有大量的数据进行测试。当然,任何文本可能看像恩的base64 codeD文本,但随机文本满足要求的概率是相当低的。

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.

校正:要获得字符集的测试,你需要通过真正作为第二个参数base64_de code在为了得到严格的检查。此外,它似乎并不即使在严格模式在意填充,所以如果你的base64 EN codeD的内容应该是正确填补你可能还需要检查(检查其长度为四的倍数)

Correction: To get the character set test, you'll need to pass true as the second argument to base64_decode in order to get strict checking. Also, it doesn't seem to care about padding even in strict mode, so if your base64 encoded content is supposed to be correctly padded you might also want to check that (check that its length is a multiple of four).

这篇关于检测base64编码在PHP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆