0

I'd like to store user id's in google analytics however since they have a policy of not allowing this, I want to encrypt some text in php and be able to decrypt when needed(The User Id).

The catch is I'd like it to be as short as possible. How can I achieve this. I'm not worried at all for this to be secure. Just need to mask it some fashion.

KingKongFrog
  • 13,946
  • 21
  • 75
  • 124
  • 1
    possible duplicate of [How do you Encrypt and Decrypt a PHP String?](http://stackoverflow.com/questions/16600708/how-do-you-encrypt-and-decrypt-a-php-string) – Artjom B. Sep 11 '15 at 07:45

1 Answers1

0

The simple and most seure solution to encrypting URL parameters is to not encrypt them at all. Instead use a randomly generated unique value, store it in another column in the desired database table, and select based on that.

For example:

function generateToken()
{
    return strtr('+/', '-_', base64_encode(random_bytes(9)));
}

And when retrieving:

$data = $db->prepare("SELECT * FROM users WHERE selector = ?")->execute([
    $_GET['user_token']
]);
Scott Arciszewski
  • 33,610
  • 16
  • 89
  • 206