How PHP implements form forms across domains "2 Ways" _php tips

Source: Internet
Author: User
Tags curl php code php database php introduction

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.

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.