Table of Contents
用户登录页面
Home Backend Development PHP Tutorial PHP实例实现验证码功能

PHP实例实现验证码功能

Jun 23, 2016 pm 01:49 PM
php Function Example Verification code

大家都知道,验证码在网站注册,登录以及很多的时候都是必备的功能。没有了验证码,网站的安全性在很大程度上就受到了威胁。所以说,验证码是网站开发过程中不可或缺的一个功能,验证码的效果直接影响到网站的安全性。(这里博主也顺便吐槽一下,验证码这功能着实蛋疼,动不动就要输个验证码,看着真心烦。前段时间12306还出了个动态验证码,尼玛我想说全宿舍没有人能输入正确。只希望赶快出个替代性的功能,结束这个操蛋功能,我想没几个用户喜欢整天对着验证码吧)。好了,废话不说了。

先用思维导图理清整体思路:

在auth.php中使用GD库进行画图,布置验证码背景,将随机生成的字符串添加到背景中,同时将字符串存到session中,保证在任何页面中都可以抠出这验证码。在form.html中写个简单的登录表格,重点是有验证码环节,用PHP实例实现验证码功能标签加载auth.php中的验证码。然后将数据提交到submit.php页面。在submit.php中,首先进行验证码验证(这里不讨论username和password),看session中存的字符串和form.html提交上来的验证码的字符串是否相同,在这里之前为了不区分大小写,一律将字符串转为小写。若字符串相同,则进行页面跳转。若不同则返回登录页面。

1.auth.php

<?phpheader ("content-type:text/html;charset=utf-8");//开启sessionsession_start();//准备画布$im=imagecreatetruecolor(50,25);//准备涂料$black=imagecolorallocate($im,0,0,0);$gray=imagecolorallocate($im,200,200,200);//背景填充imagefill($im,0,0,$gray);//文字居中$x=(50-10*4)/2;$y=(25-5)/2+5;//准备文字$arr=array_merge(range(0,9),range('a','z'),range('A','Z'));shuffle($arr);$str=implode(array_slice($arr,0,4));//把$str放入session中,方便所有页面中调用$_SESSION['vstr']=$str;$file="fonts/simsun.ttc";imagettftext($im,10,0,$x,$y,$black,$file,$str);//输出到浏览器上或保存起来header("content-type:image/png");imagepng($im);//关闭画布imagedestory($im);?>
Copy after login

(1)header头部前面不能够有任何的输出

(2)在使用session之前必须开启session

2.form.html

 	<meta charset="utf-8">	<title>登录</title>	<style>		table{						border-collapse:collapse;		}		img{			margin-top:5px;		}	</style>  	<h2 id="用户登录页面">用户登录页面</h2>	<hr>	
Copy after login
姓    名:
密    码:
验证码: PHP实例实现验证码功能
(1)method的提交方式为post

(2)尽量保证文本框和验证码的协调

3.submit.php

<?phpheader ("content-type:text/html;charset=utf-8");session_start();$code=strtolower($_POST['vcode']);$str=strtolower($_SESSION['vstr']);	if($code==$str){	//页面跳转	echo "<script>location='http://www.baidu.com'";}else{	echo "<script>alert('验证码输入错误');</script>";	echo "<a href="form.html">返回登录页面</a>";		}?>
Copy after login
(1)使用strtolower()将字符串全部转为小写,就免去了验证码区分大小写的麻烦过程。

(2)使用if语句进行判断两个字符串相等是否为true。

(3)里面加了一点js的东西,在<script>location=''</script>的引号中添加需要跳转的地址,就能直接跳转。当然用header()也行,但是header()前面不能有输出,局限性太大。最好还是用js。






Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

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

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles