问题
I have a page that receive via GET a base64 encoded data, it print data in an input hidden and it pass via get to another page.
The problem is this: when i pass
"Peuq/0X4XhFV+XNa8T06qFrP8lRadORUQBGJ1w6D4m33Jaqx/skKDEJIxjldBrcklboL/uB4C65cjz3BHMPmd3moEJ4GTK5k5Jwf9Ny4BA467bwgeaHJuOS+CjwFlIOzrhSWHTMVl4zWVvwMauuFAjhuMjOOj0/X5L12IcwGTTqLgHo"
via GET it becomes
"Peuq/0X4XhFV XNa8T06qFrP8lRadORUQBGJ1w6D4m33Jaqx/skKDEJIxjldBrcklboL/uB4C65cjz3BHMPmd3moEJ4GTK5k5Jwf9Ny4BA467bwgeaHJuOS CjwFlIOzrhSWHTMVl4zWVvwMauuFAjhuMjOOj0/X5L12IcwGTTqLgHo"
...so openssl can't work. How can I solve it?
回答1:
Certain characters in the URL are special, like +
which stands for a space. To send arbitrary data via the URL, you need to URL escape it to avoid characters contained in the data being recognized as "special characters". Since you're putting the data into HTML, you also need to HTML-escape it to avoid characters in the URL-encoded data being recognized as special HTML characters. Hence:
$data = /* some data */;
$base64Data = base64_encode($data);
$urlData = urlencode($base64Data);
$htmlData = htmlspecialchars($urlData);
printf('<input type="hidden" value="%s" name="pass-it-on">', $htmlData);
回答2:
Base64 can output +
signs which are interpreted as spaces when sent in the URL. You can use urlencode to mitigate this:
<?php
$base64_data = $_GET['base64'];
$url_data = urlencode($base64_data);
$field_data = htmlspecialchars($url_data);
printf('<input type="hidden" value="%s" name="pass-it-on">', $field_data);
?>
On page two:
<?php
$base64_data = $_GET['pass-it-on'];
$real_data = base64_decode($base64_data);
?>
Note that there's no need to decode the htmlspecialchars
ur urlencode
calls since this is done automatically.
来源:https://stackoverflow.com/questions/11208214/passing-base64-encoded-data-in-php