Home >Backend Development >PHP Tutorial >PHP implements mixed alphanumeric verification code
The verification code effect is as shown in the figure:
Verification code calling address: Application\Home\Controller\CodeController.class .php
Vendor('Vcode.Vcode', '', '.class.php'); $config = array("width" => 100, "height" => 36, "count" => 4, "str" => 2); //配置 $vcode = new \Vcode($config); $vcode->getCode(); //获取验证码 $vcode->getImg(); //输出图片 exit;
(Related free learning video tutorial sharing: php video tutorial)
The verification code picture is as follows:
<img src="__APP__/code/" id="code" onclick="changeCode($('#code'))"/ alt="PHP implements mixed alphanumeric verification code" >
JS passed Add a random number Math.random() to the suffix to refresh the verification code
function changeCode(obj) { obj.attr("src", '__APP__/code/?' + Math.random()); }
Check whether the verification code is entered correctly
<input type="text" id="input_code" class="input"/> <input type="button" value="提交" class="btn" onclick="checkCode()"/> function checkCode() { $.post("__APP__/Code/check", {code: $("#input_code").val()}, function(data) { if (data == '1') { alert("验证码正确!"); } else { alert("验证码错误!"); } }, "json") }
Compare the parameter code passed by PHP verification with the verification code stored in the current session. If it is correct, it will return 1, if it is wrong, it will be -1
public function check() { $code = I('post.code'); if (strtolower($code) == $_SESSION["sucaihuo_code"]) { echo "1"; } else { echo "-1"; } }
Recommended related articles and tutorials:php tutorial
The above is the detailed content of PHP implements mixed alphanumeric verification code. For more information, please follow other related articles on the PHP Chinese website!