Siwa
Siwa

Reputation: 103

String Encode, Array Decode

I'm new to PHP, I started about 3 weeks ago.

I have a string, which is used with $_POST to pass it to another page, the second page uses $_GET to get these url and split it as desired.

My problem is that, in my first page I use a String, and I want to encrypt it, so that I can pass it as a plan text. In the second page I must decrypt it and get it as an array.

So is there any encryption method or function I can use which is compatible with $_POST ( so I can send it to another page ) and decrypt it as an array ?

I need this method, because the second page is actually connecting to website and is a payment method. So i don't want users to manually edit the url and lower the amount of $ for the product they get.

tnx for your help.

Upvotes: 0

Views: 348

Answers (3)

Javaguru
Javaguru

Reputation: 910

What you probably want to do is pass the contents of the users cart (i.e. the items he'd like to order) to the payment site. Therefore, you should create a form like:

<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>

On the server in the file "URL/to/paymentPage.php" you can access the items using the following code:

<?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;
?>

where the function getPriceFromDB actually retrieves the price for the item/product with the id $itemID from your database or elsewhere... :)

However, the user items are usually stored in the session, and, therefore, there is no need to submit the again.. ;)

Upvotes: 0

Jeroen
Jeroen

Reputation: 13257

Despite not fully understanding what you're trying to achieve, you can use base64 encoding:

$encoded_string = base64_encode ($string);

$decoded_string = base64_decode ($encoded_string);

Upvotes: 0

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324750

You're thinking about this wrong. You NEVER trust information coming from the user's side.

For example, if your user sends a form that says what item they want, DO NOT include the price in the form. Instead, get the price from the server (database), where it can be trusted.

Upvotes: 3

Related Questions