字符串编码,数组解码

时间:2012-05-12 14:47:17

标签: php

我是PHP的新手,我大约3周前开始。

我有一个字符串,它与$ _POST一起用于将其传递到另一个页面,第二个页面使用$ _GET来获取这些URL并根据需要将其拆分。

我的问题是,在我的第一页中我使用了一个字符串,我想加密它,以便我可以将它作为计划文本传递。在第二页中,我必须解密它并将其作为数组。

那么我可以使用哪些加密方法或函数与$ _POST兼容(所以我可以将它发送到另一个页面)并将其解密为数组?

我需要这种方法,因为第二页实际上是连接到网站而且是一种付款方式。所以我不希望用户手动编辑网址并降低他们获得的产品的金额。

tnx为您提供帮助。

3 个答案:

答案 0 :(得分:3)

你在考虑这个错误。您永远不会信任来自用户方的信息。

例如,如果您的用户发送的表单中显示了他们想要的项目,请不要在表单中包含价格。相反,从服务器(数据库)获取可信任的价格。

答案 1 :(得分:0)

尽管还没有完全理解你想要实现的目标,但你可以使用base64编码:

$encoded_string = base64_encode ($string);

$decoded_string = base64_decode ($encoded_string);

答案 2 :(得分:0)

您可能想要做的是将用户购物车的内容(即他要订购的商品)传递到付款网站。 因此,您应该创建一个表单,如:

<form action="URL/to/paymentPage.php" method="post">
<!-- Item 1 -->
<input type="hidden" name="items[0]" value="productID1"/>
<input type="hidden" name="quantity[0]" value="quantity1"/>
<!-- Item 2 -->
<input type="hidden" name="items[1]" value="productID2"/>
<input type="hidden" name="quantity[1]" value="quantity2"/>
<!-- ... -->
<!-- Item n -->
<input type="hidden" name="items[n]" value="productIDn"/>
<input type="hidden" name="quantity[n]" value="quantityn"/>

<input type="submit" value="Order"/>
</form>

在文件“URL / to / paymentPage.php”中的服务器上,您可以使用以下代码访问这些项目:

<?php
$items = $_POST['items']; // Array of items ..
$quantities = $_POST['quantity']; // The array of quantities for each item ..

// Calculate the total price ..
$totalPrice = 0;
foreach($items as $idx => $itemID) {
  if($quantities[$idx]>0) {
    totalPrice += getPriceFromDB($itemID) * $quantities[$idx];
  }
}

echo 'Total Price to pay: '.$totalPrice;
?>

其中函数getPriceFromDB实际从数据库或其他地方检索具有id $ itemID的商品/产品的价格...... :)

但是,用户项通常存储在会话中,因此无需再次提交..;)

相关问题