php防止伪造的数据从URL提交方法_php技巧

WBOY
Release: 2016-05-17 08:40:27
Original
910 people have browsed it

针对伪造的数据从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;>
Copy after login

这个方法只能防止手动在浏览栏上输入的URL。
事实上只要在服务器上构造出一个指向该URL的链接(比如在发贴时加入超链),再点击,这个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);
}
Copy after login

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template