Question

I want to generate a unique string, but don't want the string to be too long (like max 10-12 chars) but I would like to generate this string off passed in data such as the user id + perhaps the time().

Something like:

$foo = sha1($user_id . time());

..would work if the output string wasn't so long.

It's not for security reasons so doesn't matter if it can be decoded or not, it's just an identifier.

Is there anything I can use - I don't have to pass data into it, I just need to make sure it's unique and only want alphanumeric characters.

Was it helpful?

Solution

For an easy solution based in your idea, you could use the hex representation of your values:

$foo = sprintf ("%x%x", $user_id, time());

or

$foo = dechex($user_id) . dechex(time());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top