Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial php 图片验证码用javascript验证

php 图片验证码用javascript验证

Jun 23, 2016 pm 02:19 PM

本帖最后由 cyyjm 于 2013-08-15 11:48:37 编辑

现在我的图片验证是交到后台验证,感觉影响查询速度。如何交给javascript验证?
php生成页面代码如下:
<?php session_start(); //生成验证码图片 Header("Content-type: image/PNG"); $im = imagecreate(44,18); // 画一张指定宽高的图片 $back = ImageColorAllocate($im, 245,245,245); // 定义背景颜色 imagefill($im,0,0,$back); //把背景颜色填充到刚刚画出来的图片中 $vcodes = ""; srand((double)microtime()*1000000); //生成4位数字 for($i=0;$i<4;$i++){ $font = ImageColorAllocate($im, rand(100,255),rand(0,100),rand(100,255)); // 生成随机颜色 $authnum=rand(1,9); $vcodes.=$authnum; imagestring($im, 5, 2+$i*10, 1, $authnum, $font); } $_SESSION['VCODE'] = $vcodes; for($i=0;$i<100;$i++) //加入干扰象素 { $randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255)); imagesetpixel($im, rand()%70 , rand()%30 , $randcolor); // 画像素点函数 } ImagePNG($im); ImageDestroy($im); ?>
Copy after login


前台样式:
		 <form name="form1" method="post" action="719.php" >			   <p><label>电脑编号:</label><input name="username" type="text" size="20"></p>			   <p class="prmt"><label>用户密码:</label><input name="pass" type="password" size="20">  <input type="submit" name="Submit" value="查 询"></p>			   <p class="prmt"><label>验 证 码:</label><input name="ask" type="text" size="6"><img  src="/static/imghw/default1.png"  data-src="muth.php"  class="lazy"  id="refresh"  onclick="document.getElementById('refresh')."/ alt="php 图片验证码用javascript验证" ></p>		</form> 
Copy after login

这样,后台里就能通过开启session_start();并与$_SESSION['VCODE']判断验证码是否正确。
我如何能够通过javascript做验证,而不需要php后台来验证。

回复讨论(解决方案)

通过javascript验证可以轻松的绕过或者说破解,不建议

一切用户能操作的验证 都是不靠谱的

那这样?先跳到中间页面,这个页面就只进行验证码判断,如果正确,再跳转到页面输出结果,否则,给出提示,回到原来那里输入页面?

sesssion 保存在服务端,前台如何能获取得到?

通过javascript做验证???
那要验证码干什么?

我通过js验证,只是不想用户又再点多一下,跳回来只是为了再输入正确的验证码,直接就在点表单的时候就验证了。

我有点看明白了  楼主你是提交的时候把验证码 用户名密码一起提交的 并且之前没有判断验证码是否正确,所以担心输错了又要重新输入用户名密码

一般这里采用ajax来做,当验证码输入框失去焦点后,获取输入的内容传递到后台进行对比,如果正确就在输入框右边打个勾 反制则打个X提示验证码错误.

输入正确后再一起提交到后台进行下一步的处理.


那我该怎么做呢?我什么都只是懂一点点

首先你得先了解一下 ajax 更便捷的是从jquery的ajax学起   20分钟内就能见效

http://www.baidu.com/s?wd=jquery+ajax    今天我也在纠结别的问题  不能帮你写全套代码了...

ajax完破

前面也遇到这个问题,后台判断验证码觉得麻烦。想用js,可是得不到验证码,后来想到把session中的验证码放到一个隐藏的标签里面,再用js。真心不好使啊,获得的验证码是上一次的,或者是刷新之前的,总是滞后,后来放弃了。

前面也遇到这个问题,后台判断验证码觉得麻烦。想用js,可是得不到验证码,后来想到把session中的验证码放到一个隐藏的标签里面,再用js。真心不好使啊,获得的验证码是上一次的,或者是刷新之前的,总是滞后,后来放弃了。
这个问题仔细想想就知道是什么原因了,你表单中隐藏标签中的值是页面初始化的值,而给用户看到的图形验证码的值是又新生成的,并且重新设置了验证码session值,所以是滞后一步。楼主的问题就像前面网友说使用ajax发起判断,具体的触发方式,我觉得是按键弹起的时候,加上验证码当时输入的长度来合理的触发ajax事件。
比如4位长的验证码,按键弹起时判断长度,到了4长度就ajax判断输入正确没有。

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

What are Enumerations (Enums) in PHP 8.1? What are Enumerations (Enums) in PHP 8.1? Apr 03, 2025 am 12:05 AM

The enumeration function in PHP8.1 enhances the clarity and type safety of the code by defining named constants. 1) Enumerations can be integers, strings or objects, improving code readability and type safety. 2) Enumeration is based on class and supports object-oriented features such as traversal and reflection. 3) Enumeration can be used for comparison and assignment to ensure type safety. 4) Enumeration supports adding methods to implement complex logic. 5) Strict type checking and error handling can avoid common errors. 6) Enumeration reduces magic value and improves maintainability, but pay attention to performance optimization.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

See all articles