Creating The Email Hash
- Trim leading and trailing whitespace from an email address
- Force all characters to lower-case
- md5 hash the final string
As an example, let’s say we start with “MyEmailAddress@example.com ” (note the trailing space which our hypothetical user entered by mistake). If we md5 encode that string directly, we get the following (in PHP):
echo md5( "MyEmailAddress@example.com " );
// "f9879d71855b5ff21e4963273a886bfc"
If we now run that same email address through the above process, you will see that we get a different result (again in PHP):
$email = trim( "MyEmailAddress@example.com " ); // "MyEmailAddress@example.com"$email = strtolower( $email ); // "myemailaddress@example.com" echo md5( $email ); // "0bc83cb571cd1c50ba6f3e8a78ef1346"
This can easily be combined into a single line:
echo md5( strtolower( trim( "MyEmailAddress@example.com " ) ) );
You can even write a trusty function and call it:
get_gravatar();
it all starts by using the principles above in PHP and do the following:
function get_gravatar($email, $default, $size="80") { return '<img src="http://www.gravatar.com/avatar.php?gravatar_id='.md5(strtolower(trim($email))).'
&default='.$default.'&size='.$size.'&rating=PG" width="'.$size.'px" height="'.$size.'px" />'; }
Next request a Gravatar Profile Using PHP
I am going to show you how to get Gravatar profile data as a serialized PHP string.
Using the steps above, I am going to create the Base Request:
- Create a valid email hash :
echo md5( strtolower( trim( "MyEmailAddress@example.com " ) ) ); - BUild the URL to request the Profile Page
- Append
.PHPto that URL to indicate that the results should be serialized in PHP format.
The result should be http://www.gravatar.com/205e460b479e2e5b48aec07710c08d50.php
So, requesting this profile in the document should look like:
In The Browser
The result is a serialized string that must be formated. I use PortableContact.net . Feel free to check it out.
An Example of the Gravatar Profile Request
This example (written in PHP, and assuming you can file_get_contents()) just loads up my profile in serialized PHP, then unserializes it and outputs my name:
<?php $str = file_get_contents( 'http://www.gravatar.com/205e460b479e2e5b48aec07710c08d50.php' ); $profile = unserialize( $str ); if ( is_array( $profile ) && isset( $profile['entry'] ) ) echo $profile['entry'][0]['displayName']; ?>







Great site. A lot of useful information here. I’m sending it to some friends!
Thanks … I will be adding to this post on how to create the Gravatar profile too.
Stay tuned.
Christian