PHP prevents spoofed data from URL submission method _php tips

Source: Internet
Author: User

For spoofed data submitted from a URL, the following code is first checked from the previous page source:

<?/*php method to prevent data submission outside the station * *
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 ("Warning!)" You are submitting data from the outside! Please terminate immediately! "); 
}
? >

This method only prevents URLs that are entered manually on the navigation bar.
In fact, as long as a link to the URL is constructed on the server (for example, by adding a hyperlink to a post) and then clicked, the check doesn't work at all.

At present, it is more reliable to transmit important data by post method.
You can insert some hidden text in the form to pass data.
Or use Ajax to submit data from the client to the server using the following method.

* * Create XHR Object
/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 ("Your browser does not support ajax! ");
}
/* Use AJAX to transfer data to page 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!=) return;
  Xhr.send (query_string);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.