ThinkPHP中的image類中已經集成了驗證碼的製作,非常的方便。
使用方法如下:
首先在當前控制器中引用驗證碼:
這個時候我們就可以在模板中引用驗證碼了,但爲了驗證碼不過期,我們在模板中加上一個刷新驗證碼的JS函數。
123456789101112131415161718 <script type="text/javascript">function $(id) { return document.getElementById(id);}function fleshVerify(){//重載驗證碼var timenow = new Date().getTime();document.getElementById('verifyImg').src= '__URL__/verify/'+timenow;}</script> <form method="post" name="form1" action="__URL__/writes">請輸入驗證碼:<input type="text" name="seccode" id="seccode" size="11" /> <A HREF="javascript:fleshVerify()"><img SRC="__URL__/verify/" name="verifyImg" BORDER="0" id="verifyImg" title="如果您無法識別驗證碼,請點圖片更換" /></a><input type="submit" name="Submit2" value="提交" /></form>當然最後,我們需要在提交表單內容後對首先對驗證碼進行判斷:
1234567 public function writes() { $seccode=trim($_POST['seccode']); if(md5($seccode)!=Session::get('verify'))$this->error('驗證碼錯誤!!!'); ........}到這裏,整個驗證碼的判斷過程就結束了。