1

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.

ManelPNavarro
  • 579
  • 7
  • 21

3 Answers3

1

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']));
Álvaro González
  • 142,137
  • 41
  • 261
  • 360
0

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
Community
  • 1
  • 1
rhazen
  • 173
  • 8
  • Given `method="get"`, `$_POST['...']` will just trigger an undefined index notice. And `json_encode()` will not generate the expected output format. – Álvaro González Jul 14 '16 at 06:05
0

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);
Mohammad H.
  • 856
  • 9
  • 19