Reputation: 579
I need to be able to provide custom URL's from html forms with the data encoded in base64.
For example:
<form name="test" action="test.php" method="get">
<input type="text" name="category" />
<input type="text" name="gender" />
<input type="text" name="quantity" />
</form>
And get an URL:
test.php?Y2F0ZWdvcnk9c3BvcnRzJmdlbmRlcj1mZW1hbGUmcXVhbnRpdHk9NTA=
Thanks.
Upvotes: 1
Views: 3966
Reputation: 1046
You do not need to use another array just simple use this code
$codedData = base64_encode(json_encode($_POST));
$decodedData = json_decode(base64_decode($orderEncode) , true);
Upvotes: 0
Reputation: 146460
If we assume that Y2F0ZWdvcnk9c3BvcnRzJmdlbmRlcj1mZW1hbGUmcXVhbnRpdHk9NTA=
is the expected output, we'll start by decoding it to see what it looks like:
category=sports&gender=female&quantity=50
That's exactly the query string of your GET form, so we can grab the value right from from $_SERVER['QUERY_STRING']
.
The built-in function to encode is 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(). So we have all the bricks:
$url = 'test.php?' . rawurlencode(base64_encode($_SERVER['QUERY_STRING']));
Upvotes: 1
Reputation: 173
You should probably post the form first and then do the following:
// 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();
I used json_encode because base64 alone can alter the meaning of your data. Checkout this article on that subject: Passing base64 encoded strings in URL
Then on test.php:
$data = json_decode( base64_decode( $_GET['data'] ) );
// Sanitize data again
Upvotes: 0