使用 get 方法编码的表单数据库 base64

Form data base64 encoded with get method

我需要能够使用 base64 编码的数据从 html 表单中提供自定义 URL。

例如:

<form name="test" action="test.php" method="get">
 <input type="text" name="category" />
 <input type="text" name="gender" />
 <input type="text" name="quantity" />
</form>

并获得 URL:

test.php?Y2F0ZWdvcnk9c3BvcnRzJmdlbmRlcj1mZW1hbGUmcXVhbnRpdHk9NTA=

谢谢。

您可能应该先 post 表单,然后执行以下操作:

// Sanitize data

$urlData = $array();

$urlData['category'] = $_POST['category'];
$urlData['gender']   = $_POST['gender'];
$urlData['quantity'] = $_POST['quantity'];

$urlData = base64_encode( json_encode( $urlData ) );

header("Location test.php?data=". $urlData ."");
exit();   

我使用了 json_encode 因为 base64 本身就可以改变数据的含义。 查看有关该主题的这篇文章:Passing base64 encoded strings in URL

然后 test.php:

$data = json_decode( base64_decode( $_GET['data'] ) );
// Sanitize data again

如果我们假设 Y2F0ZWdvcnk9c3BvcnRzJmdlbmRlcj1mZW1hbGUmcXVhbnRpdHk9NTA= 是预期的输出,我们将首先对其进行解码以查看其外观:

category=sports&gender=female&quantity=50

这正是您的 GET 表单的查询字符串,因此我们可以直接从 $_SERVER['QUERY_STRING'].

中获取值

编码的内置函数是base64_encode() and we can concatenate strings with a built-in operator as well. Last not but least, we can encode a URL component with another built-in function called rawurlencode()。所以我们有所有的积木:

$url = 'test.php?' . rawurlencode(base64_encode($_SERVER['QUERY_STRING']));

您不需要使用另一个数组 只需简单地使用此代码

$codedData = base64_encode(json_encode($_POST));
$decodedData = json_decode(base64_decode($orderEncode) , true);