Home > Backend Development > PHP Tutorial > PHP sets multiple domain names to allow cross-domain access

PHP sets multiple domain names to allow cross-domain access

藏色散人
Release: 2023-04-07 12:20:01
forward
5250 people have browsed it

Set multiple domain names for PHP language to allow cross-domain access

Server variables:

Server variables are stored in the $_SERVER array. There is a special Key value: HTTP_ORIGIN. This key will only have a value when it is cross-domain, and it will be an empty string when it comes from the same origin

Response header settings allow access to a certain domain name: access-control-allow-origin

The header function can be set to allow cross-domain access for a certain domain name, in the form of header('access_control_allow_origin:*').

Posted code:

$allow_origin = array(
    'a.baidu.com',
    'b.baidu.com',
);
//跨域访问的时候才会存在此字段
$origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '';  
if (in_array($origin, $allow_origin)) {
    header('Access-Control-Allow-Origin:' . $origin);
} else {
    return;
}
Copy after login

note: When requesting the interface through the interface tool, the $_SERVER['HTTP_ORIGIN'] variable is also an empty string.

Recommended tutorial: PHP tutorial

The above is the detailed content of PHP sets multiple domain names to allow cross-domain access. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template