PHP:从base64_decode重构关联数组的好方法 [英] PHP: Good way to reconstruct associative array from base64_decode

查看:61
本文介绍了PHP:从base64_decode重构关联数组的好方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想 base64_encode 通过URL发送的参数.

I want to base64_encode the parameters I send over the url.

发送:

<?php 
    $product = array("productID"=>"13776", "name"=>"something", "availability"=>"1000");
    $url_details = "?id=" . base64_encode(http_build_query($product));
?>

<a href="details.php<?php echo $url_details; ?>" class="btn btn-primary btn-lg" role="button">Details</a>

接收:

<?php
    $details = base64_decode($_GET["id"]);
    // $product = what is the best way to reconstruct the array from $details?
?>
<p>
    Name: <?php echo $products["name"]; ?>
    ...
</p>

编码会破坏数组,是否有方便的方法可以再次从字符串中创建关联数组?

The encoding destroys the array, is there a convenient way to make an associative array out of the string again?

(编码的url不是敏感信息,但我仍然不希望它在url中完全可读.如果有比我正在做的更好的在页面之间传递数据的方法,请告诉我)

(the encoded url is not sensitive information, but I still do not want it to be flat out readable in the url. If there is a better way to pass this data between pages than what I am doing, let me know)

推荐答案

parse_str是http_build_query的反函数,因此要恢复您的数据:

parse_str is the inverse of http_build_query, so to recover your data:

parse_str(base64_decode($_GET["id"]), $details);

请注意,如果仅使用一个参数调用parse_str,则是有害的.

Note that parse_str is harmful if called with only one argument.

顺便说一句,您可能不希望将此类信息放入URL开头,因为它可以很容易地透露给第三方通过引荐来源"标头.

And as an aside, you may not want to put this kind of information into the URL to begin with, since it can be easily disclosed to third parties, via the Referrer header, for instance.

这篇关于PHP:从base64_decode重构关联数组的好方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆