you generate a warning because of an undefined var..
change this line
imagettftext ($image, 30, 0, 10, 40, $color, $dir."arial.ttf", $_SESSION['random_code']);
to this one
imagettftext ($image, 30, 0, 10, 40, $color, $dir."arial.ttf", $_SESSION['rand_code']);
also check, if the font file is really available and readable, or you also will get an warning