ManelMusti ManelMusti - 4 months ago 21
PHP Question

Form data base64 encoded with get method

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.

Answer

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