PHP md5() problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hrysax
Forum Newbie
Posts: 2
Joined: Fri Oct 03, 2008 1:38 pm

PHP md5() problem

Post by hrysax »

Hi,

This is a weird issue. Every once in awhile, my code will return the exact same "$linkId" value for the following:

$linkId = substr(md5(uniqid(rand(), true) + $user_id), 0, 20) . '-' . substr(md5(uniqid(rand(), true) + $user_id), 13, 32);

It always returns: 9517fd0bf8faa655990a-655990a4dffe358e13e

I have no clue as to why this specific code would always return the same value, even for different users. This is a HUGE issue for me. Thanks for the help!
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: PHP md5() problem

Post by onion2k »

It's always that regardless of what $user_id is.

Here we go...

Code: Select all

uniqid(rand(), true) + $user_id
always returns "INF". Remove the "+ $user_id" and it'll work.

EDIT: I do wonder though.. what are you trying to do? What's wrong with just using uniqid() on it's own? Or an MD5 hash of it. Why are you taking two halves of two random hashes? That won't make it any more random.
hrysax
Forum Newbie
Posts: 2
Joined: Fri Oct 03, 2008 1:38 pm

Re: PHP md5() problem

Post by hrysax »

Thanks for your reply!

The $user_id is the unique user ID for each user so I thought it may help to make it a little more random. I just combined two hashes cause I wanted it 40 chars long.

BUT, I use that same code around the entire site to generate special little unique hash codes and they always seems to work :(

Its so random though. Sometimes it will work and give different results, and sometimes, no matter who the user is, will generate the same result.
phppucci
Forum Newbie
Posts: 17
Joined: Fri Oct 03, 2008 2:27 pm

Re: PHP md5() problem

Post by phppucci »

If you want 40 chars, use sha1() instead of md5()
Post Reply