Php prevents counterfeit data from being submitted from the URL

Source: Internet
Author: User
This article mainly introduces php's method to prevent counterfeit data from being submitted from URLs. For more information, see

This article mainly introduces php's method to prevent counterfeit data from being submitted from URLs. For more information, see

The following code first checks the source of the previous page for the submission of forged data from a URL:

<? /* PHP method to prevent data from being submitted outside the site */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 outside! Please terminate now! ") ;}?>

This method can only prevent the URL entered manually on the browser bar.
In fact, as long as the server constructs a link pointing to the URL (such as adding a hyperlink to the post), and then click it, this Check will not work at all.

Currently, it is relatively reliable to transmit important data using the POST method.
You can insert some hidden text into the form to transfer data.
Alternatively, use Ajax to submit data to the server from the client.

/* Create an 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 pass data to the 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 );}

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.