【发布时间】:2016-01-05 03:13:17
【问题描述】:
此 PHP 允许用户提交带有二进制或纯文本 URL 的表单。然后将 URL 转换为纯文本,我使用 cURL 加载响应,然后将其转换回二进制文件。如果您想知道,它来自一个二进制翻译器,它可以翻译文本 -> 二进制和二进制 -> 文本,但也接受 URL。
问题:如您所见,二进制 URL 被翻译成文本,然后传递给 cURL。明文值存储在$newtext 中。我可以通过一些调试确认binaryToText() 确实像宣传的那样工作。纯文本 URL(请参阅 if 语句的 else 部分)已成功设置,但转换后的二进制 URL 未成功设置。
示例
例如$text = "http://google.co.uk";
(isBinary($text) == false)
curl_setopt($ch, CURLOPT_URL, $text);
例如$text = "01101000011101000111010001110000001110100010111100101111011001110110111101101111011001110110110001100101001011100110001101101111001011100111010101101011"(相同的谷歌网址)
(isBinary($text) == true)
$newtext = binaryToText($text);
curl_setopt($ch, CURLOPT_URL, $newtext);
echo curl_error($ch); -> 输出“未设置 URL!”
问题出在哪里……不过我看不到。
代码清单
$text = $_POST["text"];
if(startsWith($text, "http://") || startsWith($text, "https://") || startsWith($text, textToBinary("http://")) || startsWith($text, textToBinary("https://"))) {
// URL - accepts binary (prefered), or plain text url; returns binary.
$ch = curl_init();
if(isBinary($text)) {
$newtext = binaryToText($text);
curl_setopt($ch, CURLOPT_URL, $newtext);
} else {
curl_setopt($ch, CURLOPT_URL, $text);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "MY USERAGENT");
if(isset($_GET["r"])) curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$result=curl_exec($ch);
echo curl_error($ch);
curl_close($ch);
echo textToBinary($result);
} else if...
功能
function isBinary($input) {
return !preg_match('/[^(0|1)]/', $input); // contains nothing but 0 and 1
}
function binaryToText($input) {
$return = '';
$chars = explode("\n", chunk_split(str_replace("\n", '', $input), 8));
$_I = count($chars);
for($i = 0; $i < $_I; $return .= chr(bindec($chars[$i])), $i++);
return $return;
}
编辑:
$newtext 的输出包含在此输出中:
text: 01101000011101000111010001110000001110100010111100101111011001110110111101101111011001110110110001100101001011100110001101101111001011100111010101101011
newtext: http://google.co.uk
No URL set!00000000
从这里:
if(isBinary($text)) {
echo "text: ".$text."\n";
$newtext = binaryToText($text);
echo "newtext: ".$newtext."\n";
curl_setopt($ch, CURLOPT_URL, $newtext);
} else {
curl_setopt($ch, CURLOPT_URL, $text);
}
【问题讨论】:
-
检查
$newtext = binaryToText($text)的值 -
嘿,我添加了
$newtext的输出,因为它从回声中出现(请参阅我答案底部的编辑)