Skip to content

Instantly share code, notes, and snippets.

@tranepura
Last active November 18, 2015 22:36
Show Gist options
  • Select an option

  • Save tranepura/3dc49474f422c82fdd5f to your computer and use it in GitHub Desktop.

Select an option

Save tranepura/3dc49474f422c82fdd5f to your computer and use it in GitHub Desktop.
Script to decode 64base encoded query string to a object for database insert
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
?>
<?php
#strings for testing
$url = "www.memberadvantage.com.au/activate?g=83&mshipNo=123456&Firstname=Sarah&Lastname=Brook&Email=sBrook@memberadvantage.com.au";
$url2 ="g=83&mshipNo=123456&Firstname=Sarah&Lastname=Skirgård&Email=sBrook@memberadvantage.com.au";
$encoded_s= "Zz04MyZtc2hpcE5vPTEyMzQ1NiZGaXJzdG5hbWU9U2FyYWgmTGFzdG5hbWU9U2tpcmfDg8KlcmQmRW1haWw9c0Jyb29rQG1lbWJlcmFkdmFudGFnZS5jb20uYXU";
$queryString[] = $_GET ;
#This function encode string to base64 and removes special chars in encoded string
function base64url_encode($data) {
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
//echo (base64_encode($data)).'<br>raw';
}
#This function decode encoded string data using above function
function base64url_decode($data) {
return base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT));
}
$encoded_string = base64url_encode($url2);
//echo 'encoded : '.$encoded_string.'<br>';
foreach ($queryString[0] as $key => $value) {
$encoded_string =$key;
}
//var_dump($key);
$decoded_string = base64url_decode($encoded_string);
//echo '<br> decoded :'.$decoded_string;
//$pieces = explode("&", $decoded_string);
$pieces = preg_split("/&/", $decoded_string);
//var_dump($pieces);
$object = new stdClass();
$exlploded_array = Array();
foreach ($pieces as $key =>$value) {
$pieces_two = preg_split("/=/", $value);
//var_dump($pieces_two[0]);
$exlploded_array[$pieces_two[0]] =$pieces_two[1];
//var_dump($exlploded_array);
}
$exploded_array_object = (object)$exlploded_array;
var_dump ($exploded_array_object);
//echo $encoded_string;
//accessing the object
echo '<br>Group: '.$exploded_array_object->g .'<br>';
echo 'mshipNo: '.$exploded_array_object->mshipNo .'<br>';
echo 'Firstname: '.$exploded_array_object->Firstname .'<br>';
echo 'Lastname: '.$exploded_array_object->Lastname .'<br>';
echo 'Email: '.$exploded_array_object->Email .'<br>';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment