Home  >  Article  >  Backend Development  >  A brief analysis of solutions to PHP cross-domain problems

A brief analysis of solutions to PHP cross-domain problems

藏色散人
藏色散人forward
2023-03-22 16:14:363063browse

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:*");

2. Allow Specify domain name access

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

Set the request method to allow access:

1, one or more

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

2, all

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

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");

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,//允许跨域请求 ... ... ... })

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!

Statement:
This article is reproduced at:juejin.im. If there is any infringement, please contact admin@php.cn delete