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.

有帮助吗?

解决方案

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());
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top