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 );}