captcha.php(PHP产生验证码并储存Session):
| //开启Sessionsession_start(); |
captcha-form.html(Web表单验证):
| <html><head> <meta charset="utf-8"> <title>验证码title>head> <body> <form method="post" action="./captcha-result.php"> <p> 验证码图片: <img id="captcha-img" border="1" src="https://www.mshxw.com/skin/sinaskin/image/nopic.gif0"> <a href="javascript:void(0);" onclick="getCaptchaImg();">看不清a> p> <p>请输入图片中的内容:<input id="authcode" type="text" name="authcode" value="">p> <p><input type="submit" value="提交" style="padding:6px 20px;">p> form> <script> //动态获取验证码 function getCaptchaImg(){ //从服务器获取新的验证码 document.getElementById('captcha-img').src='https://www.mshxw.com/skin/sinaskin/image/nopic.gif'+Math.random(); //清空文本框里已输入的内容 document.getElementById('authcode').value=""; } script> body>html> |
captcha-result.php(PHP判断验证码是否正确):
| //验证验证码是否正确if(isset($_REQUEST['authcode'])){ |
所用到的函数原型:
| //imagecreatetruecolor() 返回一个图像标识符,代表了一幅大小为 x_size 和 y_size 的黑色图像。resource imagecreatetruecolor ( int $width , int $height ); |