Php cross-origin form submission methods-PHP source code

Source: Internet
Author: User
We know that the browser generally does not support cross-origin form submission because of security issues, but sometimes we need it again. Next let's take a look at an example of cross-origin form submission. We know that the browser generally does not support cross-origin form submission because of security issues, but sometimes we need it again. Next let's take a look at an example of cross-origin form submission.

Script ec (2); script

For security reasons, direct cross-origin access is not allowed. The following describes two cross-origin methods.

1. Use 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://111cn.net", array ('name' => "tank "));


In the past, many people used curl to capture the email address book, but it is no longer possible. Haha.

Ii. Use jquery form and ajax to submit

1. Download jquery. form. js

2. js Code


$ ('# Testform'). submit (function (){
$ (This). ajaxSubmit ({
Type: 'post', // submit method get/post
DataType: "json", // data type
Url: 'Your url', // url to be submitted
Success: function (data) {// data stores the data returned after submission, which is generally json data
// Data can be processed here
Alert ('submission successful! ');
}
$ (This). resetForm (); // reset the form after submission
});
Return false; // prevents automatic form submission events
});

3. php code

Header ("Access-Control-Allow-Origin: *"); // cross-Origin permission settings, Allow all

Header ("Access-Control-Allow-Origin: http://www.111cn.net"); // only Allow 111cn.net cross-Origin data submission

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.