>  Q&A  >  본문

인증 코드에 숫자가 하나만 표시되는 이유는 무엇입니까?

//이번 강의의 코드는 이렇습니다. 여러번 확인했는데 오류는 없는데 인증코드에 숫자가 1개만 나와요... 왜 이런 일이 일어나는지


<?php

//인증코드 생성 배경

header('Content-type:image/jpeg');

//배경 이미지 크기

$width=60;

$height=15 ;

//캔버스 만들기

$img=imagecreatetruecolor($width, $height);

//색상 할당

$white=imagecolorallocate($img, 0xff, 0xff, 0xff);

//색 채우기 캔버스로

imagefill($img, 0, 0, $white);

//인증 코드 값 생성

$chars='1234567890';

$chars_len=strlen( $chars);

$code_len =4;//인증 코드 길이

$code="";//초기값

for ($i=1; $i < $code_len; ++$i) {

$rand=mt_rand(0 ,$chars_len-1);//4개의 숫자를 무작위로 꺼냅니다

$code=$rand;//가져온 숫자를 함께 연결합니다

}

//세션에 저장하고 검증을 사용하세요

session_start() ;

$_SESSION['ver_code']=$code;

//문자열 색상을 무작위로 지정

$str_color=imagecolorallocate($img, mt_rand(0,255), mt_rand(0,255), mt_rand (0,255));

//문자열의 중앙 표시 계산

//문자열의 크기

$font=5;

//캔버스 크기

$img_w=imagesx($img);

$img_h=imagesy($img) ;

//글꼴 크기

$font_w=imagefontwidth($font);

$font_h=imagefontheight($font);

//문자열 크기

$code_w=$ font_w*$code_len;

$ code_h=$font_h;

$x=($img_w-$code_w)/2;

$y=($img_h-$code_h)/2;

//인증 출력 캔버스에 코드

imagestring( $img, $font, $x, $y, $code, $str_color);

//직접 출력

imagejpeg($img);

imagedestroy($img);


?>


丫头丫头2345일 전2972

모든 응답(5)나는 대답할 것이다

  • 王承毅

    王承毅2018-04-21 12:33:36

    for($i=0;$i<$code_len;$i++){

    $rand=mt_rand(0,$chars_len-1);//숫자를 무작위로 빼내기

    $code.=$rand; // 빼낸 숫자 4개가 합쳐져있네요

    }

    커넥터가 빠졌네요

    회신하다
    0
  • 丫头

    괜찮아요, 고마워요

    丫头 · 2018-04-22 10:53:02
  • 丫头

    丫头2018-04-21 11:28:11

    <?php

    //인증코드 생성 배경

    header('Content-type:image/jpeg');

    //배경 이미지 크기

    $width=60;

    $height=15 ;

    //캔버스 만들기

    $img=imagecreatetruecolor($width, $height);

    //색상 할당

    $white=imagecolorallocate($img, 0xff, 0xff, 0xff);

    //색 채우기 캔버스에

    imagefill($img, 0, 0, $white);

    //인증 코드 값 생성

    $chars='1234567890';

    $chars_len=strlen($ chars);

    $code_len =4;//인증 코드 길이

    $code="";//초기값

    for ($i=1; $i < $code_len; ++$i) {

    $rand=mt_rand(0 ,$chars_len-1);//숫자 4개를 무작위로 꺼냅니다

    $code.=$rand;//가져온 숫자를 함께 연결합니다

    }

    //세션에 저장하고 검증 사용

    session_start( );

    $_SESSION['ver_code']=$code;

    //문자열 색상을 무작위로 지정

    $str_color=imagecolorallocate($img, mt_rand(0,255), mt_rand(0,255), mt_rand (0,255))

    //문자열의 중앙 표시 계산

    //문자열의 크기

    $font=5;

    //캔버스 크기

    $img_w=imagesx($img);

    $img_h=imagesy($img );

    //글꼴 크기

    $font_w=imagefontwidth($font);

    $font_h=imagefontheight($font);

    //문자열 크기

    $code_w=$ font_w*$code_len;

    $code_h=$font_h;

    $x=($img_w-$code_w)/2;

    $y=($img_h-$code_h)/2;

    //인증 출력 캔버스에 코드

    imagestring ($img, $font,$x.$y.$code,$str_color);

    //직접 출력

    imagejpeg($img);

    imagedestroy($img);


    ?>

    회신하다
    0
  • 丫头

    丫头2018-04-21 11:21:00

    TIM图片20180421112016.png

    이것으로 바꾸면 3자리 숫자가 됩니다...

    회신하다
    1
  • 王承毅

    for 루프에 오류가 있습니다. 다음과 같아야 합니다. for($i=0;$i<$code_len;$i++){ $rand=mt_rand(0,$chars_len-1);//무작위로 숫자 꺼내기 $code.=$rand; //꺼낸 네 자리 숫자를 이어붙입니다. }

    王承毅 · 2018-04-21 12:35:29
  • 취소회신하다