search
Homephp教程php手册php防止站外远程提交表单例子

防止站长提交表单无非就是对每一次打开表单或提交数据都会需要加一个token来进行验证了,这个其实与验证码做法没什么两样了,下面来看几个防止站外远程提交表单的例子.

例子一,我们每一次打开提交页面生成一个token然后保存在session中,当表单提交时我们来判断当前的token值与session是否一致,如果是的就是正常提交否则就是无效提交了.

PHP实例代码如下:

<?php      
	session_start();      
	      
	if ($_POST[&#39;submit&#39;] == "go"){      
	    //check token      
	    if ($_POST[&#39;token&#39;] == $_SESSION[&#39;token&#39;]){      
	        //strip_tags      
	        $name = strip_tags($_POST[&#39;name&#39;]);      
	        $name = substr($name,0,40);      
	        //clean out any potential hexadecimal characters      
	        $name = cleanHex($name);      
	        //continue processing....      
	    }else{      
	        //stop all processing! remote form posting attempt!      
	    }      
	}      
	      
	$token = md5(uniqid(rand(), true));      
	$_SESSION[&#39;token&#39;]= $token;      
	      
	 function cleanHex($input){      
	    $clean = preg_replace("![\][xX]([A-Fa-f0-9]{1,3})!", "",$input);      
	    return $clean;      
	}      
	      
	<form action="<?php echo $_SERVER[&#39;PHP_SELF&#39;];" method="post">      
	<p><label for="name">Name</label>      
	<input type="text" name="name" id="name" size="20" maxlength="40"/></p>      
	<input type="hidden" name="token" value="<?php echo $token;"/>     //开源代码phprm.com 
	<p><input type="submit" name="submit" value="go"/></p>      
	</form> 

还有一种比较明显的做法就是利用验证码,这种验证码的方式与其它的方式是一样的,下面看个简单的例子.

增加验证码:表单提交时候增加验证码,可以有效防止灌水机提交数据,但是随着图形图像识别程序变的更加强大,验证码识别也不断的在提高他的难度,有些验证码甚至加入了声音的识别,一些小站点可以采用这样的方式,代码如下:

if($_POST['vcode'] != get_vcode())
{
    exit('验证码校验失败,无法入库');
}

具体的例子就不介绍了网上很多验证的相关例子.

本文地址:

转载随意,但请附上文章地址:-)

Statement
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)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools