The example in this article describes how PHP implements form forms across domains. Share to everyone for your reference, specific as follows:
Sometimes we do not allow form data to be submitted directly across domains for security reasons, if we have this requirement? Here we introduce two cross-domain approaches to solving direct cross-domain problems.
Let's take a look at two ways to cross-domain submit form for PHP
First, through the 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"));
Before many people use curl to grasp, the mailbox address Book, but now is not OK. Ha ha.
Ii. using jquery Form,ajax to submit
1. Download Jquery.form.js
2. JS Code
$ (' #testform '). Submit (function () {
$ (this). Ajaxsubmit ({
type: ' Post ',//Submit Way Get/post
DataType: "JSON", Data type
URL: ' Your URL ',///need to submit URL
success:function (data) {//file to save the returned after submission, generally JSON data
//here can be related to data Handle
alert (' Commit success! ');
}
$ (this). Resetform (); Reset the form after submission
});
return false; Prevent forms from automatically submitting events
});
3. PHP Code
Header ("access-control-allow-origin:*"); Cross-domain permission settings, allow all
headers ("access-control-allow-origin:http://www.test.com");///Allow only test.com data to be submitted across domains
More about PHP Interested readers can view the site topics: "PHP Design Security Course", "PHP Security Filtering Skills Summary", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", "PHP basic Grammar Introductory Course", " PHP Operations Office Document Tips summary (including word,excel,access,ppt), "PHP Introduction to Object-oriented Programming", "PHP string (String) Usage Summary", "Php+mysql Database Operations Introductory Tutorial" and " A summary of common PHP database operations tips
I hope this article will help you with the PHP program design.