base64_decode showing corrupted characters

263 views Asked by At

I am building a hyperlink that includes a base64 encoded set of parameters as shown below:

$params = base64_encode("member_id={$recipient_id}&api_key=".SECRET_KEY);
$link   = HOST_ADDRESS."test.php?k=" . $params;

When the link is executed, the following code runs:

// get the encoded string from the link parameter 
$link_parm = $_GET['k'];
$link = substr($link_parm, 0);

// url encode the string to ensure all special characters convert properly - attempt to stop errors
urlencode($link);

// decode the rest of the link 
$decoded_link = base64_decode($link);

// get the remaining data elements from the link parameter 
$msg_data = preg_split( "/[&=]/", $decoded_link);

On occasion, the $msg data is corrupted and looks like this:

member_id=167œÈ&api_key=secretkey

As you can see the member id is corrupted.

Can someone please help me understand what may be causing this?

Thanks.

1

There are 1 answers

0
Ro Achterberg On

For starters, there are a few problems with this beside the issue you describe.

  1. What are you trying to do using $link = substr($link_parm, 0);? This could just be written as $link = $link_parm;. Or, you could of course just do $link = $_GET['k']; or even just use $_GET['k'].
  2. urlencode($link); does nothing as you're not catching its result. The argument is not passed by reference.
  3. Your "attempt to stop errors" should probably be handled differently. By throwing an error when you're receiving unexpected input, for instance.