PHP implementation method for submitting form forms across domains

Source: Internet
Author: User
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.

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.