---
title: "TP5 验证码"
author: "onestopweb"
date: "2017-07-23T17:14:45Z"
tags: []
categories: ["编程语言"]
slug: "tp5"
draft: false
lastmod: "2017-07-23T17:14:45Z"
id: 2386499
url: "https://onestopweb.iteye.com/blog/2386499"
---

application\\index\\controller\\Index.php

```
<?php
namespace app\index\controller;
use think\Controller;
class Index extends Controller
{
    public function index()
    {
        return $this->fetch();
    }
    public function check($code='')
    {
        $captcha = new \think\captcha\Captcha();
        if(!$captcha->check($code)) {
            $this->error('验证码错误');
        }else{
            $this->success('验证码正确');
        }
    }
}
```

 

application\\index\\view\\index\\index.html

```
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<FORM method="post" action="{:url('check')}">
	<p>
		<input name="code" type="text" placeholder="请输入验证码" style="width: 100px;">
		<span>
			<img class="captcha" src="{:captcha_src()}" alt="请点击刷新验证码"
			onclick="this.src='{:captcha_src()}'+'?'+Math.random()" />
		</span>
	</p>
	<p style="margin-top: 30px;">
		<input type="submit" class="button" value="提交">
	</p>
</FORM>
</body>
</html>
```

 

效果图：  
![](http://dl2.iteye.com/upload/attachment/0126/2012/51910593-08d6-3043-9822-ec6c756b7040.png)