>  기사  >  백엔드 개발  >  PHP 인증코드 기능 구현

PHP 인증코드 기능 구현

巴扎黑
巴扎黑원래의
2016-11-23 13:32:12831검색

/**

*인증코드 이미지 생성

*/

공개 함수 actionVerfiycode() {

헤더( "콘텐츠 유형: 이미지/gif" );

$border = 0; //테두리 1 필수 여부: 0, 아니요

$how = 4; //인증 코드 자릿수

$w = $how * 15;

$h = 20; //이미지 높이

$fontsize = 5; //글꼴 크기

$alpha = "abcdefghijkmnopqrstuvwxyz"; letter

$number = "0123456789"; //인증코드 내용 2: 숫자

$randcode = ""; //인증코드 문자열 초기화

srand ( ( double ) microtime () * 1000000 ); //난수 시드 초기화

$im = ImageCreate ($w, $h); //확인 이미지 생성

$bgcolor = ImageColorAllocate ($im , 255, 255, 255 ); //배경색 설정

ImageFill ( $im, 0, 0, $bgcolor ) // 배경색 채우기

if ($border) {

$black = ImageColorAllocate ( $im, 0, 0, 0 ); //테두리 색상 설정

ImageRectangle ( $im, 0, 0, $w - 1, $h - 1, $ black ); //테두리 그리기

}

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

$alpha_or_number = mt_rand ( 0, 1 ); // 문자 또는 숫자

$str = $alpha_or_number ? $alpha : $number;

$which = mt_rand ( 0, strlen ( $str ) - 1 ) ; //문자 가져오기

$code = substr ( $str, $which, 1 ) // 문자 가져오기

$j = ! j + 15; //문자 위치 그리기

$color3 = ImageColorAllocate ( $im, mt_rand ( 0, 100 ), mt_rand ( 0, 100 ), mt_rand ( 0, 100 ) ) // 문자 색상은 be

ImageChar ( $im, $fontsize, $j, 3, $code, $color3 ); // 문자 그리기

$randcode .= $code // 확인 코드 문자열 추가; bit by bit

}

for($i = 0; $i < 5; $i ++) //배경 간섭선 그리기

{

$color1 = ImageColorAllocate ( $im, mt_rand ( 0, 255 ), mt_rand ( 0, 255 ), mt_rand ( 0, 255 ) ) //간섭선 색상

ImageArc( $im, mt_rand( - 5, $w ), mt_rand( - 5, $h ), mt_rand( 20, 300 ), mt_rand( 20, 200 ), 55, 44, $color1 ); 🎜>

}

for($i = 0; $i < $how * 40; $i ++) //배경 간섭점 그리기

{

$color2 = ImageColorAllocate ( $im, mt_rand ( 0, 255 ), mt_rand ( 0, 255 ), mt_rand ( 0, 255 ) ) //간섭점 색상

ImageSetPixel ( $im, mt_rand ( 0, $w ) , mt_rand ( 0, $h ), $color2 ); //간섭점

}

$session=new CHttpSession;

$session- >open() ;

Yii::app()->session->add('randcode',$randcode);

/*그리기 끝*/

Imagegif ( $im );

ImageDestroy ( $im );

/*그리기 끝*/

}


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
이전 기사:PHP 등록 코드다음 기사:PHP 등록 코드