views:

34

answers:

1

I'm using a captcha script for a form. Strangely the image will frequently fail to render the first character of the string of random characters. This pattern will last for about 8-12 refreshed pages, before the expected behavior is reestablished.

I know the string contains the full set of four characters because I've debugged this with a print statement to reveal the last session var:
1) image is loaded missing the first character;
2) on refresh the last four character string is revealed on the page with print;
3) comparrison confirms the first character was not rendered in the last captcha image;

This script uses sessions. The script also sets the color of the background and color of the text from two non-overlaping value ranges. This only occurs on the remote shared host site and not my local test site.

I'm thinking my remote site is adding latency somewhere. Or maybe the PHP tags used to generate the image (imagecolorallocate( ), imagecreate( ), imagepng( ), imagettftext( ), imagettfbbox( )) have some built in latency I have not accounted for. Maybe there is a way to tune the php.ini or .htaccess files?

UPDATE=

Test Results ...Code:

01  --------------------------------------
02  test     $_SESSION VAR     IMG VALUE
03  --------------------------------------
04  0                                  2UV
05  1            Q2UV             UMV
06  2            CUMV             KON
07  3            5KON             D93
08  4            MD93             4GH
09  5            T4GH             8BH
10  6            V8BH             UBJ
11  7            WUBJ             AMN9
12  8            AMN9 ...Next 50+ are OK

As you can see its always the first character.

PHP Code:

13        $char_spacing = 200 / 4; 
14        $font_list = array("arial.ttf", "castelar.ttf", "gibli.ttf", "lfaxi.ttf");
15        /* toggle for speed test $font_list = array("arial.ttf", "arial.ttf", "arial.ttf", "arial.ttf"); */
16        //start image creation 
17        if (!function_exists('imagecreate') || !function_exists("imagepng") || !function_exists("imagecolorallocate") || !function_exists("imagettftext") || !function_exists("imagettfbbox") || !function_exists("imagedestroy")) 
18        { 
19            return false; 
20        } 
21        $image = @imagecreate(200, 50); 
22        if(!$image){ 
23            return false; 
24        } 
25        $background_color = imagecolorallocate($image, rand(150,255), rand(150,255), rand(150,255));    
26        //draw in some noise 
27        for($i = 0; $i < 15; $i++){ 
28            $rand_colour = imagecolorallocate($image, rand(120, 250), rand(120, 250), rand(120, 250)); 
29            imageline($image, rand(0, 200), rand(0, 50), rand(0, 200), rand(0, 50), $rand_colour); 
30        } 
31     
32        //generate random string 
33        for ($s = '', 
34            $i = 0, 
35            $z = strlen( 
36                "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789" 
37            )-1; 
38            $i != 4; 
39            $x = rand(0,$z), 
40            $s .= "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789"{ 
41                $x 
42            }, 
43            $i++ 
44        ); 
45        $_SESSION[$captcha_det['session_id']] = $s; 
46        for($i = 0; $i < strlen($s); $i++){ 
47            $font = $font_list[array_rand($font_list)]; 
48            $colour = imagecolorallocate($image, rand(0, 100), rand(0, 100), rand(0, 100)); 
49            $font_size = rand(16, 22); 
50            $angle = rand(-30, 30); 
51            $char_dets = imagettfbbox($font_size, $angle, $font, $s[$i]); 
52            $x = ($char_spacing / 4) + ($i * $char_spacing); 
53            $y = (50 / 2) + (($char_dets[2] - $char_dets[4]) / 4) + rand(5, 10); 
54            imagettftext($image, $font_size, $angle, $x, $y, $colour, $font, $s[$i]); 
55        } 
56        imagepng($image);