search
Homephp教程php手册php防止伪造数据从地址栏URL提交的方法,伪造url

php防止伪造数据从地址栏URL提交的方法,伪造url

针对伪造的数据从URL提交的情况,首先是一个检查前一页来源的如下代码:

<&#63;/*PHP防止站外提交数据的方法*/
function CheckURL(){
$servername=$_SERVER['SERVER_NAME']; 
$sub_from=$_SERVER["HTTP_REFERER"]; 
$sub_len=strlen($servername); 
$checkfrom=substr($sub_from,7,$sub_len); 
if($checkfrom!=$servername)die("警告!你正在从外部提交数据!请立即终止!"); 
}
&#63;>

这个方法只能防止手动在浏览器地址栏上输入的URL。

事实上只要在服务器上构造出一个指向该URL的超链接(www.jb51.net)比如在发贴时加入超链,再点击,这个Check就完全不起作用了。
目前觉得还是用POST的方法传递重要数据比较可靠。
可以在form中插入一些隐藏的text用于传递数据。
或者使用下面的方法,利用Ajax从客户端向服务器提交数据。

/*创建XHR对象*/
function createXHR()
{
if (window.XMLHttpRequest){
var oHttp = new XMLHttpRequest();
return oHttp;
} 
else if (window.ActiveXObject){
var versions = ["MSXML2.XmlHttp.6.0","MSXML2.XmlHttp.3.0"];
for (var i = 0; i < versions.length; i++){
try {
var oHttp = new ActiveXObject(versions[i]);
return oHttp;
} catch (error) {}
}
}
throw new Error("你的浏览器不支持AJAX!");
}
/*用AJAX向page页面传递数据*/
function ajaxPost(url,query_string='')
{
var xhr;
xhr = createXHR();
xhr.open('POST',url,false);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=gb2312");
xhr.onreadystatechange = function(){if (xhr.readyState == 4)if (xhr.status != 200)return;}
xhr.send(query_string);
}

PHP代码怎防止外部网站提交表单到本站

试一试加一个验证码
 

php防止站外直接提交网址的方法

在第2个页面加个变量传过去,再判断这个变量来决定是不是禁止访问.
index.php

$i=$_GET['i'];
$servername=$HTTP_SERVER_VARS['SERVER_NAME'];
$sub_from=$HTTP_SERVER_VARS["HTTP_REFERER"];
$sub_len=strlen($servername);
$checkfrom=substr($sub_from,10,$sub_len);
if($checkfrom!=$servername and !$i){
echo("<script>alert(&#39;请不要从外部提交数据!&#39;);window.location.href=&#39;login.php&#39;;<&#47;script>"); <br />exit; <br />} <br />?> <br /><br />p.php<br /><script>window.setTimeout("location=&#39;index.php?i=1&#39;",20000)<&#47;script> <br /><br />问题是解决了,但是觉得不是很好。呵呵<br />也是一个思路吧,个人挺反感url后带变量的,换成隐藏表单POST过去也是可以的。。不过就不能用js自动跳转了。<br/>  </script>

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

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools