在php中實現驗證碼還是很方便的,關鍵點在于掌握php gd庫與session的用法。
縱觀網上php 生成驗證碼的例子,無不是php gd庫與session相結合,并利用php 生成隨機數的方法來完成。
PHP驗證碼,可以分為很多種,包括 php 圖片驗證碼,php 隨機驗證碼,以及php 中文驗證碼等,根據不同的應用場合來使用不同的驗證碼。
這里分享一個php數字驗證碼,供大家參考。
4位數字驗證碼
- /*
- *Filename:authpage.php
- */
- session_start();
- //srand((double)microtime()*1000000);
- $authnum=$_SESSION['authnum'];
- //驗證用戶輸入是否和驗證碼一致
- if(isset($_POST['authinput']))
- {
- if(strcmp($_POST['authinput'],$_SESSION['authnum'])==0)
- echo"驗證成功!";
- else
- echo"驗證失??!";
- }
- //生成新的四位整數驗證碼
- //while(($authnum=rand()%10000)<1000);
- ?>
- <formaction=test4.phpmethod=post>
- <table>
- 請輸入驗證碼:<inputtype=textname=authinputstyle="width:80px"><br>
- <inputtype=submitname="驗證"value="提交驗證碼">
- <inputtype=hiddenname=authnumvalue=<?echo$authnum;?>>
- <imgsrc=authimg.php?authnum=<?echo$authnum;?>>
- </table>
- </form>
新聞熱點
疑難解答