检测 PHP 中的 base64 编码?
有没有办法检测 PHP 中的字符串是否经过 base64_encoded() ?
我们正在将一些存储从纯文本转换为 Base64,其中一部分存储在需要更新的 cookie 中。如果文本尚未编码,我想重置他们的 cookie,否则就不要管它。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
对于对已经回答的问题的迟回复表示歉意,但我认为 base64_decode($x,true) 不是解决此问题的足够好的解决方案。事实上,可能没有一个非常好的解决方案可以针对任何给定的输入。例如,我可以将大量错误值放入 $x 中,而不会得到错误的返回值。
我认为除了严格的返回值检查之外,您还需要进行解码后验证。最可靠的方法是您可以解码然后对照一组已知的可能值进行检查。
精度低于 100% 的更通用的解决方案(对于较长的字符串更接近,对于短字符串不准确)是检查输出以查看是否有许多字符超出 utf-8(或您使用的任何编码)字符的正常范围。
看这个例子:
结果:
所以你可以这样做:
你可能应该使用解码值的mean()而不是max(),我在这个例子中只使用了max(),因为遗憾的是没有内置的PHP 中的平均值()。您针对阈值(例如 200)使用什么度量(平均值、最大值等)取决于您估计的使用情况。
总之,唯一的胜利之举就是不玩。我会尽量避免首先识别 Base64。
Apologies for a late response to an already-answered question, but I don't think base64_decode($x,true) is a good enough solution for this problem. In fact, there may not be a very good solution that works against any given input. For example, I can put lots of bad values into $x and not get a false return value.
I think that in addition to the strict return value check, you'd also need to do post-decode validation. The most reliable way is if you could decode and then check against a known set of possible values.
A more general solution with less than 100% accuracy (closer with longer strings, inaccurate for short strings) is if you check your output to see if many are outside of a normal range of utf-8 (or whatever encoding you use) characters.
See this example:
Results:
So you may do something like this:
You should probably use the mean() of the decoded values instead of the max(), I just used max() in this example because there is sadly no built-in mean() in PHP. What measure you use (mean,max, etc) against what threshold (eg 200) depends on your estimated usage profile.
In conclusion, the only winning move is not to play. I'd try to avoid having to discern base64 in the first place.
http://php.net/manual/en/function.base64-decode .php#81425
http://php.net/manual/en/function.base64-decode.php#81425
我遇到了同样的问题,我最终得到了这个解决方案:
I had the same problem, I ended up with this solution:
迟到总比不到好:您可以使用
mb_detect_encoding()
来查明编码的字符串是否看起来是某种文本:更新 对于那些喜欢简短的人
Better late than never: You could maybe use
mb_detect_encoding()
to find out whether the encoded string appears to have been some kind of text:UPDATE For those who like it short
我们可以将三件事组合成一个函数来检查给定的字符串是否是有效的 Base 64 编码。
We can combine three things into one function to check if given string is a valid base 64 encoded or not.
我正要在 php 中构建一个 base64 切换,这就是我所做的:
它非常适合我。
以下是我对此的完整想法: http://www.albertmartin.de /blog/code.php/19/base64-detection
在这里你可以尝试一下:http:// www.albertmartin.de/tools
I was about to build a base64 toggle in php, this is what I did:
It works perfectly for me.
Here are my complete thoughts on it: http://www.albertmartin.de/blog/code.php/19/base64-detection
And here you can try it: http://www.albertmartin.de/tools
如果输入不是有效的 Base64 编码数据,base64_decode() 将不会返回 FALSE。使用
imap_base64()
代替,如果 $text 包含 Base64 字母表之外的字符,则返回 FALSEimap_base64() 参考
base64_decode() will not return FALSE if the input is not valid base64 encoded data. Use
imap_base64()
instead, it returns FALSE if $text contains characters outside the Base64 alphabetimap_base64() Reference
这是我的解决方案:
if(empty(htmlspecialchars(base64_decode($string, true)))) {
返回假;
}
如果解码后的
$string
无效,则返回 false,例如:“node”、“123”、“ ”等。Here's my solution:
if(empty(htmlspecialchars(base64_decode($string, true)))) {
return false;
}
It will return false if the decoded
$string
is invalid, for example: "node", "123", " ", etc.可能这并不完全是您所要求的。但希望它对某人有用。
就我而言,解决方案是使用 json_encode 然后使用 base64_encode 对所有数据进行编码。
该值可以根据您的需要进行存储或使用。
然后检查这个值是否不仅仅是一个文本字符串,而是您简单使用的编码数据
,或者
感谢该线程中所有先前答案的作者:)
May be it's not exactly what you've asked for. But hope it'll be usefull for somebody.
In my case the solution was to encode all data with json_encode and then base64_encode.
this value could be stored or used whatever you need.
Then to check if this value isn't just a text string but your data encoded you simply use
or alternatively
Thanks to all previous answers authors in this thread:)
通常,base64 中的文本没有空格。
我使用了这个功能,对我来说效果很好。它测试字符串中的空格数是否小于 20 中的 1 个。
例如:每 20 个字符至少有 1 个空格 --- ( space / strlen ) < 0.05
Usually a text in base64 has no spaces.
I used this function which worked fine for me. It tests if the number of spaces in the string is less than 1 in 20.
e.g: at least 1 space for each 20 chars --- ( spaces / strlen ) < 0.05
您最好的选择是:
Your best option is: