A brief analysis of solutions to PHP cross-domain problems

藏色散人
Release: 2023-04-11 12:58:01
forward
3064 people have browsed it

This article brings you relevant knowledge about PHP interviews. It mainly talks about the solutions to PHP cross-domain problems. Friends who are interested can take a look below. I hope it will be helpful to you. help.

A brief analysis of solutions to PHP cross-domain problems

Set the domain names that are allowed to be accessed:

1. Allow all domain names to access

header("Access-Control-Allow-Origin:*");
Copy after login

2. Allow Specify domain name access

header( 'Access-Control-Allow-Origin:a.test.com' );
Copy after login

Set the request method to allow access:

1, one or more

header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE');
Copy after login

2, all

header('Access-Control-Allow-Methods:*');
Copy after login

Set the request header for access on demand

header("Access-Control-Allow-Headers:DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type, Accept-Language, Origin, Accept-Encoding, Access-Token");
Copy after login

Can be placed on the entry file of the PHP framework (note: pay attention to the code sequence)

Set in the front-end ajax:

$.ajax({ type: "POST", crossDomain: true,//允许跨域请求 ... ... ... })
Copy after login

Recommended learning: "PHP video tutorial"

The above is the detailed content of A brief analysis of solutions to PHP cross-domain problems. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:juejin.im
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template