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



我需要能够提供来自html表单的自定义URL,其中包含以base64编码的数据。

例如:

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

并获取一个网址:

test.php?Y2F0ZWdvcnk9c3BvcnRzJmdlbmRlcj1mZW1hbGUmcXVhbnRpdHk9NTA=

谢谢。

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

category=sports&gender=female&quantity=50

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

要编码的内置函数是 base64_encode(),我们也可以使用内置运算符连接字符串。最后但最不重要的一点是,我们可以使用另一个名为rawurlencode()的内置函数对URL组件进行编码。所以我们有所有的砖块:

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

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

// 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 就可以改变数据的含义。查看有关该主题的这篇文章:在 URL 中传递 base64 编码字符串

然后在测试中.php:

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

不需要使用其他数组只是简单地使用此代码

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

最新更新