Home  >  Article  >  Backend Development  >  PHP implements mixed alphanumeric verification code

PHP implements mixed alphanumeric verification code

王林
王林forward
2020-01-25 21:30:082994browse

PHP implements mixed alphanumeric verification code

The verification code effect is as shown in the figure:

PHP implements mixed alphanumeric verification code

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($(&#39;#code&#39;))"/ 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", &#39;__APP__/code/?&#39; + 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 == &#39;1&#39;) { 
      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(&#39;post.code&#39;); 
    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!

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete