[SOLVED]Why does md5 return different hash code

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
tralli10
Forum Newbie
Posts: 4
Joined: Mon Aug 23, 2010 4:59 am

[SOLVED]Why does md5 return different hash code

Post by tralli10 »

Hi I am having trouble understanding this behavior. I'm using a cms system called cms made simple (I've tried posting on their forum but get no replies). One of it's addons has a function that creates an url (it's inner workings is unknown to me, but I know it returns an url string). Lets say I call the function and it returns the url: http://someurl.com and store it in $url, now when I call md5($url) it returns invalid hash code but when I echo $url and copy/paste the string into md5: md5("http://someurl.com") it returns the expected hash value.
Now how could this be? At first I suspected encoding problems, but I've tried utf8_encode, utf8_decode, urlencode, urldecode, rawurlencode even htmlentitites, stripslashes, addslashes on $url but I don't get the correct hashcode as from "http://someurl.com"

Any ideas?
Last edited by tralli10 on Mon Sep 26, 2011 3:53 pm, edited 1 time in total.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Why does md5 return different hash code for same values?

Post by Celauran »

Any examples? Have you tried var_dump($url)?
tralli10
Forum Newbie
Posts: 4
Joined: Mon Aug 23, 2010 4:59 am

Re: Why does md5 return different hash code for same values?

Post by tralli10 »

Celauran wrote:Any examples? Have you tried var_dump($url)?
I did but it returns the same url as echo i.e:
[text]string(116) "http://vintage.is.hysir.net/index.php?m ... 31&page=31" [/text]

But when I copied the url into another variable $foo and dump both strings it shows some interesting results:
[text]
string(108) "http://vintage.is.hysir.net/index.php?m ... 31&page=31"
string(116) "http://vintage.is.hysir.net/index.php?m ... 31&page=31"
[/text]

$url length is 116 while $foo is 108
tralli10
Forum Newbie
Posts: 4
Joined: Mon Aug 23, 2010 4:59 am

Re: Why does md5 return different hash code for same values?

Post by tralli10 »

Ofcourse! each ampersand in $url was infact '$amp;' but the browser converted it to '&' before I could see it. htmlspecialchars_decode($url) fixed that.
Post Reply