This article mainly introduces PHP implementation of the form form cross-domain method, combined with examples of curl and Ajax two methods for cross-domain submission of the operation skills, the need for friends can refer to the next
The example in this article describes how PHP implements cross-domain submission form forms. Share to everyone for your reference, as follows:
Sometimes we are not allowed to submit form form data directly across domains for the sake of website security, if we have this requirement ourselves? Let's introduce two cross-domain approaches to solve direct cross-domain problems.
Let's take a look at two ways PHP submits form across domains
One, through PHP curl
function Curlpost ($url, $params) { $postData = '; foreach ($params as $k = = $v) { $postData. = $k. ' = '. $v. ' & '; } RTrim ($postData, ' & '); $ch = Curl_init (); curl_setopt ($ch, Curlopt_url, $url); curl_setopt ($ch, curlopt_returntransfer,true); curl_setopt ($ch, Curlopt_header, false); curl_setopt ($ch, Curlopt_post, Count ($postData)); curl_setopt ($ch, Curlopt_postfields, $postData); $output =curl_exec ($ch); Curl_close ($ch); return $output;} Echo curlpost ("http://test.com", Array (' name ' = "Tank"));
A lot of people used curl to catch the email address book, but now it is not possible. Ha ha.
Ii. use of jquery Form,ajax submissions
1. Download Jquery.form.js
2. JS Code
$ (' #testform '). Submit (function () { $ (this). Ajaxsubmit ({ type: ' Post ',//Submit by Get/post DataType: "JSON", Data type URL: ' Your URL ',//URL required to be submitted success:function (data) {//* * * * * * * * * * To save the file returned after the commit, typically JSON data //This can be related to data Handle alert (' Submit success! '); } $ (this). Resetform (); Reset form after submission }); return false; Prevent the form from automatically committing events});
3. PHP Code
Header ("access-control-allow-origin:*"); Cross-domain permission settings, allowing all headers ("access-control-allow-origin:http://www.test.com"); Only allow test.com to submit data across domains
Summary: The above is the entire content of this article, I hope to be able to help you learn.