Here's a simplication of your function.
Basically, I have shifted the declaration of the ranges to outside the loop, which will save you much time.
Since there are only 2 possible values to be tested, a simple if conditional is better than a switch structure.
function ghash($length = 8)
{
$h = '';
$alpha = range('a', 'z');
$numeric = range(0, 9);
for ($x = 0; $x < $length; $x++)
{
if (mt_rand(0, 1))
{
$h .= $alpha[mt_rand(0, 25)];
}
else
{
$h .= $numeric[mt_rand(0, 9)];
}
}
return md5($h);
}
That said, since you are using ghash() to provide a session key, limiting the entropy so greatly is just asking for trouble.
With the default length there is only 36**8=2821109907456 possible combinations, smaller than even the cube root of the number of possible MD5 hashes!
Also, while SHA1 is theoretically broken collion-wise, MD5 is even more so, so I recommend that you use SHA1 instead.
A simple solution might then be:
function ghash($salt = '')
{
return sha1($salt.mt_rand().mt_rand().mt_rand().mt_rand());
}