提共一個透明的數字驗證碼

前幾周開始挑選合適的 framwork

在 CodeIgniter & Yii 兩者之前挑來挑去的

最後 就賭上 Yii 了, 不多 Yii  的中文 文件真少 好像也推出不久

不如 CodeIgniter 的 中文文件多 , 推出也久

但既來之 則 安知 慢慢摸索了

本週終於有時間可以研究了

有用到一組 透明背景的數字驗證碼

提供出來給大家

;) 至少我自己也要有所貢獻

<?php

class CKCaptchaAction extends CCaptchaAction {

public $imageTransparent=false;	//背景是否透明

    protected function generateVerifyCode() {

	if($this-&gt;minLength &lt; 3)	$this-&gt;minLength=3;


	if($this-&gt;maxLength &gt; 10)	 $this-&gt;maxLength=10;


	if($this-&gt;minLength &gt; $this-&gt;maxLength)	$this-&gt;maxLength=&nbsp; $this-&gt;minLength;


	$length = rand($this-&gt;minLength, $this-&gt;maxLength);





	$code=&#039;&#039;;


	for($i=0; $i&lt;$length; $i++) {


		$code .= Chr(48+rand(0, 9));


	}





	return $code;

  }

protected function renderImage($code) {


		$image = ImageCreate($this-&gt;width, $this-&gt;height);


		$backColor = ($this-&gt;imageTransparent) ? ImageColorTransparent($image, ImageColorAllocate($image, (int)($this-&gt;backColor%0x1000000/0x10000), (int)($this-&gt;backColor%0x10000/0x100), $this-&gt;backColor%0x100)) : ImageColorAllocate($image, (int)($this-&gt;backColor%0x1000000/0x10000), (int)($this-&gt;backColor%0x10000/0x100), $this-&gt;backColor%0x100);//ImageColorAllocate第1次定義顏色PHP就認為是底色了


		//$backColor = ImageColorTransparent($image, ImageColorAllocate($image, (int)($this-&gt;backColor%0x1000000/0x10000), (int)($this-&gt;backColor%0x10000/0x100), $this-&gt;backColor%0x100));


		//文字顏色為藍色


		$foreColor = ImageColorAllocate($image, (int)($this-&gt;foreColor%0x1000000/0x10000), (int)($this-&gt;foreColor%0x10000/0x100), $this-&gt;foreColor%0x100);


		//圖片背景為白色


		/*


		for($i=1; $i&lt;=200; $i++) {&nbsp; &nbsp; //先用100個做測試&nbsp; 


			imageString($im, 1, mt_rand(0, $img_width), mt_rand(0, $img_height), &quot;*&quot;, imageColorAllocate($im,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255)));&nbsp; 


		}


		*/


		// 將文字放到圖片裡面並靠左上角


		ImageString($image, 5, 4, 0, $code, $foreColor);


		//輸出圖片


		header(&quot;Content-type: image/png&quot;);


		ImagePNG($image);


}

}

客位可以把它存到 \extensions\CKCaptchaAction.php

再去呼叫他就好,這裡好像沒在討論 CodeIgniter & Yii的喔

不過也好  東西 各有好壞 在用的人的不同嘛!! ;D

有演示吗?

很不错啊。把它放到extensions里共享?

class SiteController extends CController

{

public function actionIndex() {


	Yii::app()-&gt;session-&gt;open();


	$this-&gt;layout = &#039;login&#039;;


	$form=new LoginForm;


	if(isset($_POST&#91;&#039;LoginForm&#039;])) {


		// 收集用戶輸入的數據


		$form-&gt;attributes=$_POST&#91;&#039;LoginForm&#039;];


		// 驗證用戶輸入,如果無效則重定位到前個頁面


		if($form-&gt;validate())


			$this-&gt;redirect(Yii::app()-&gt;user-&gt;returnUrl);


	}


	// 顯示登陸表單


	$this-&gt;render(&#039;login&#039;,array(&#039;form&#039;=&gt;$form));


	//$this-&gt;render(&#039;login&#039;, array(&#039;user&#039;=&gt;$form));


}





public function actions() {


	return array(


		&#039;captcha&#039;=&gt; array(				


			&#039;class&#039; =&gt; &#039;application.extensions.CKCaptchaAction&#039;,	//&#039;CCaptchaAction&#039;,


			&#039;backColor&#039; =&gt; 0xc0c0c0,


			&#039;foreColor&#039; =&gt; 0x0000ff,


			&#039;height&#039; =&gt; 16,


			&#039;width&#039; =&gt; 42,


			&#039;testLimit&#039; =&gt; 2,


			&#039;maxLength&#039; =&gt; 4,


			&#039;minLength&#039; =&gt; 4,	


		 ),


	);


}

}

<?php

/**

  • LoginForm class.

  • LoginForm is the data structure for keeping

  • user login form data. It is used by the 'login' action of 'SiteController'.

*/

class LoginForm extends CFormModel

{

public $username;


public $password;


public $verifyCode;


public $rememberMe;





/**


 * Declares the validation rules.


 * The rules state that username and password are required,


 * and password needs to be authenticated.


 */


public function rules()


{


	return array(


		// username and password are required


		array(&#039;username, password, verifyCode&#039;, &#039;required&#039;),


		// password needs to be authenticated


		array(&#039;password&#039;, &#039;authenticate&#039;),


		array(&#039;verifyCode&#039;, &#039;captcha&#039;, &#039;allowEmpty&#039;=&gt; !extension_loaded(&#039;gd&#039;)),


	);


}





/**


 * Declares the attribute labels.


 * If an attribute is not delcared here, it will use the default label


 * generation algorithm to get its label.


 */


public function attributeLabels()


{


	return array(


		&#039;rememberMe&#039;=&gt;&#039;Remember me next time&#039;,


	);


}





/**


 * Authenticates the password.


 * This is the &#039;authenticate&#039; validator as declared in rules().


 */


public function authenticate($attribute,$params)


{


	if(!$this-&gt;hasErrors())&nbsp; // we only want to authenticate when no input errors


	{


		$identity=new UserIdentity($this-&gt;username, $this-&gt;password);


		$identity-&gt;authenticate();


		switch($identity-&gt;errorCode)


		{


			case UserIdentity::ERROR_NONE:


				$duration=$this-&gt;rememberMe ? 3600*24*30 : 0; // 30 days


				Yii::app()-&gt;user-&gt;login($identity,$duration);


				Yii::app()-&gt;user-&gt;returnUrl = Yii::app()-&gt;urlManager-&gt;createUrl(&#039;admin&#039;);


				break;


			case UserIdentity::ERROR_USERNAME_INVALID:


				$this-&gt;addError(&#039;username&#039;,&#039;Username is incorrect.&#039;);


				break;


			default: // UserIdentity::ERROR_PASSWORD_INVALID


				$this-&gt;addError(&#039;password&#039;,&#039;Password is incorrect.&#039;);


				break;


		}


	}


}

}