Inhaltsverzeichnis
回复内容:
Heim Backend-Entwicklung PHP-Tutorial php中使用tesseract识别验证码,并且模拟登录,验证码错误

php中使用tesseract识别验证码,并且模拟登录,验证码错误

Jun 06, 2016 pm 08:19 PM
php tesseract 模拟登录 验证码

代码如下:

<code><?php header("Content-type:text/html;charset=utf-8");
/**
 * 模拟登录
 */

//1.初始化变量
$cookie_file = tempnam('./temp','cookie');
$login_url = "http://210.32.33.91:8080/reader/redr_verify.php";//登录页面
$verify_code_url = "http://210.32.33.91:8080/reader/captcha.php";//验证码页面

//2.获取cookies
echo "正在获取COOKIE...<br>";
$curl = curl_init();
$timeout = 5;
curl_setopt($curl, CURLOPT_URL, $login_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($curl,CURLOPT_COOKIEJAR,$cookie_file); //获取COOKIE并存储
$contents = curl_exec($curl);
curl_close($curl);
//3.取出验证码
echo "COOKIE获取完成,正在取验证码...<br>";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $verify_code_url);
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_file);//保存cookie
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file);//使用cookie
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$img = curl_exec($curl);
curl_close($curl);

$codename = time();
$fp = fopen("/home/wwwroot/default/tesseract/Test/images/$codename.png","w");
echo "<img  src="/static/imghw/default1.png" data-src="/img/bVssL8" class="lazy" alt="php中使用tesseract识别验证码,并且模拟登录,验证码错误" >";
fwrite($fp,$img);
fclose($fp);
//开始识别验证码
echo "验证码取出完成,正在休眠,正在识别验证码...<br>";

passthru("/usr/bin/tesseract  /home/wwwroot/default/tesseract/Test/images/$codename.png /home/wwwroot/default/tesseract/Test/images/$codename");
$code = file_get_contents("./images/$codename.txt");

echo "验证码成功取出:$code<br>";

echo "正在准备模拟登录...<br>";

$post_url = "http://210.32.33.91:8080/reader/redr_verify.php";
//为安全性,此处密码不提供。
$post = "number=1111111&passwd=111111&captcha=$code&select=cert_no&returnUrl=";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $post_url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file);
$result=curl_exec($curl);
curl_close($curl);
echo str_replace('captcha.php','http://210.32.33.91:8080/reader/captcha.php',$result);</code>
Nach dem Login kopieren
Nach dem Login kopieren

回复内容:

代码如下:

<code><?php header("Content-type:text/html;charset=utf-8");
/**
 * 模拟登录
 */

//1.初始化变量
$cookie_file = tempnam('./temp','cookie');
$login_url = "http://210.32.33.91:8080/reader/redr_verify.php";//登录页面
$verify_code_url = "http://210.32.33.91:8080/reader/captcha.php";//验证码页面

//2.获取cookies
echo "正在获取COOKIE...<br>";
$curl = curl_init();
$timeout = 5;
curl_setopt($curl, CURLOPT_URL, $login_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($curl,CURLOPT_COOKIEJAR,$cookie_file); //获取COOKIE并存储
$contents = curl_exec($curl);
curl_close($curl);
//3.取出验证码
echo "COOKIE获取完成,正在取验证码...<br>";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $verify_code_url);
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_file);//保存cookie
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file);//使用cookie
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$img = curl_exec($curl);
curl_close($curl);

$codename = time();
$fp = fopen("/home/wwwroot/default/tesseract/Test/images/$codename.png","w");
echo "<img  src="/static/imghw/default1.png" data-src="/img/bVssL8" class="lazy" alt="php中使用tesseract识别验证码,并且模拟登录,验证码错误" >";
fwrite($fp,$img);
fclose($fp);
//开始识别验证码
echo "验证码取出完成,正在休眠,正在识别验证码...<br>";

passthru("/usr/bin/tesseract  /home/wwwroot/default/tesseract/Test/images/$codename.png /home/wwwroot/default/tesseract/Test/images/$codename");
$code = file_get_contents("./images/$codename.txt");

echo "验证码成功取出:$code<br>";

echo "正在准备模拟登录...<br>";

$post_url = "http://210.32.33.91:8080/reader/redr_verify.php";
//为安全性,此处密码不提供。
$post = "number=1111111&passwd=111111&captcha=$code&select=cert_no&returnUrl=";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $post_url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file);
$result=curl_exec($curl);
curl_close($curl);
echo str_replace('captcha.php','http://210.32.33.91:8080/reader/captcha.php',$result);</code>
Nach dem Login kopieren
Nach dem Login kopieren

2016/1/25 14:51更新

Linux的话captcha目录,cookies目录给写权限
你一步步断点调试,看你那个程序生成的结果是多少,图片是多少,

php中使用tesseract识别验证码,并且模拟登录,验证码错误
代码在:https://github.com/rainwsy/sf/tree/master/library-OCR-login


更新:
你应该
1.将验证码存下来,跟文字结果对比下,
2.对比每次的session_id是否一致
3.CURLOPT_COOKIEJAR第一次用的时候存session_id就可以了,后面的操作用CURLOPT_COOKIEFILE来读取session_id,其实你可以对比下几次请求返回header头中的session_id是否一致
我的验证码识别结果:

php中使用tesseract识别验证码,并且模拟登录,验证码错误

写了个DEMO:

指出几个问题,获取session在获取验证码的那一步一并获取就OK,没必要先取得session再取验证码,
当看到账号密码隐藏的时候我再想这是给校友回答的吗?

php中使用tesseract识别验证码,并且模拟登录,验证码错误

<code><?php require_once 'OCR.php';
$loginUrl = "http://210.32.33.91:8080/reader/redr_verify.php"; // 登录页面
$captchaUrl = "http://210.32.33.91:8080/reader/captcha.php"; // 验证码页面

$cookie_file = __DIR__ . DIRECTORY_SEPARATOR . 'cookies' . DIRECTORY_SEPARATOR . date('YmdHis') . '.txt';

// 获取验证码
$captchaString = get($captchaUrl, $cookie_file, true);
$tempCaptchaFile = __DIR__ . DIRECTORY_SEPARATOR . 'captcha' . DIRECTORY_SEPARATOR . date('YmdHis') . '.gif';
file_put_contents($tempCaptchaFile, $captchaString);
/*既然你说不是验证码的问题,我就不发出来了*/
$ocr = new OCR($tempCaptchaFile);
$captcha = $ocr->getCaptcha();

/* 开始登陆 */
$username = '用户名';
$passwd = '密码';
$postArray = [
    'number' => $username,
    'passwd' => $passwd,
    'captcha' => $captcha,
    'select' => 'cert_no',
    'returnUrl' => ''
];
$postData = http_build_query($postArray);
echo post($loginUrl, $postData, $cookie_file);

function get($url, $cookie_file, $isCookiesSave = false)
{
    // 初始化
    $curl = curl_init($url);
    $header = array();
    $header[] = 'User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36';
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    // 不输出header头信息
    curl_setopt($curl, CURLOPT_HEADER, 0);
    if ($isCookiesSave) {
        curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_file); // 存储cookies
    } else {
        curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file);
    }
    // 保存到字符串而不是输出
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    // 是否抓取跳转后的页面
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    $info = curl_exec($curl);
    curl_close($curl);
    return $info;
}

function post($url, $data, $cookie_file)
{
    // 初始化
    $curl = curl_init($url);
    $header = array();
    $header[] = 'User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36';
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    // 不输出header头信息
    curl_setopt($curl, CURLOPT_HEADER, 0);
    // 保存到字符串而不是输出
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file);
    // post数据
    curl_setopt($curl, CURLOPT_POST, 1);
    // 请求数据
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    // 是否抓取跳转后的页面
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    $response = curl_exec($curl);
    curl_close($curl);
    return $response;
}</code>
Nach dem Login kopieren

网上的评论是 识别率低

Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

AI Hentai Generator

AI Hentai Generator

Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

R.E.P.O. Energiekristalle erklärten und was sie tun (gelber Kristall)
2 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
Repo: Wie man Teamkollegen wiederbelebt
4 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Abenteuer: Wie man riesige Samen bekommt
3 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌

Heiße Werkzeuge

Notepad++7.3.1

Notepad++7.3.1

Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version

SublimeText3 chinesische Version

Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1

Senden Sie Studio 13.0.1

Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6

Dreamweaver CS6

Visuelle Webentwicklungstools

SublimeText3 Mac-Version

SublimeText3 Mac-Version

Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

CakePHP-Projektkonfiguration CakePHP-Projektkonfiguration Sep 10, 2024 pm 05:25 PM

In diesem Kapitel werden wir die Umgebungsvariablen, die allgemeine Konfiguration, die Datenbankkonfiguration und die E-Mail-Konfiguration in CakePHP verstehen.

PHP 8.4 Installations- und Upgrade-Anleitung für Ubuntu und Debian PHP 8.4 Installations- und Upgrade-Anleitung für Ubuntu und Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 bringt mehrere neue Funktionen, Sicherheitsverbesserungen und Leistungsverbesserungen mit einer beträchtlichen Menge an veralteten und entfernten Funktionen. In dieser Anleitung wird erklärt, wie Sie PHP 8.4 installieren oder auf PHP 8.4 auf Ubuntu, Debian oder deren Derivaten aktualisieren. Obwohl es möglich ist, PHP aus dem Quellcode zu kompilieren, ist die Installation aus einem APT-Repository wie unten erläutert oft schneller und sicherer, da diese Repositorys in Zukunft die neuesten Fehlerbehebungen und Sicherheitsupdates bereitstellen.

CakePHP Datum und Uhrzeit CakePHP Datum und Uhrzeit Sep 10, 2024 pm 05:27 PM

Um in cakephp4 mit Datum und Uhrzeit zu arbeiten, verwenden wir die verfügbare FrozenTime-Klasse.

CakePHP-Datei hochladen CakePHP-Datei hochladen Sep 10, 2024 pm 05:27 PM

Um am Datei-Upload zu arbeiten, verwenden wir den Formular-Helfer. Hier ist ein Beispiel für den Datei-Upload.

CakePHP-Routing CakePHP-Routing Sep 10, 2024 pm 05:25 PM

In diesem Kapitel lernen wir die folgenden Themen im Zusammenhang mit dem Routing kennen.

Besprechen Sie CakePHP Besprechen Sie CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP ist ein Open-Source-Framework für PHP. Es soll die Entwicklung, Bereitstellung und Wartung von Anwendungen erheblich vereinfachen. CakePHP basiert auf einer MVC-ähnlichen Architektur, die sowohl leistungsstark als auch leicht zu verstehen ist. Modelle, Ansichten und Controller gu

So richten Sie Visual Studio-Code (VS-Code) für die PHP-Entwicklung ein So richten Sie Visual Studio-Code (VS-Code) für die PHP-Entwicklung ein Dec 20, 2024 am 11:31 AM

Visual Studio Code, auch bekannt als VS Code, ist ein kostenloser Quellcode-Editor – oder eine integrierte Entwicklungsumgebung (IDE) –, die für alle gängigen Betriebssysteme verfügbar ist. Mit einer großen Sammlung von Erweiterungen für viele Programmiersprachen kann VS Code c

CakePHP erstellt Validatoren CakePHP erstellt Validatoren Sep 10, 2024 pm 05:26 PM

Der Validator kann durch Hinzufügen der folgenden zwei Zeilen im Controller erstellt werden.

See all articles