Home > Article > Backend Development > Can php access other web pages across domains?
PHP cannot be directly accessed across domains. If you need to access other web pages across domains, you need to set up PHP to allow cross-domain access.
Recommended: php server
In addition, cross-domain access needs to be allowed, the configuration is as follows (no other output operations are allowed before configuring the content):
//设置允许跨域的 请求源地址 //方式一: header("Access-Control-Allow-Origin: *");//允许所有地址跨域请求 //方式二: header("Access-Control-Allow-Origin: http://localhost:8080");//指定某个地址可以跨域请求,这里只能指定一个 //方式三:如果要允许多个地址跨域请求可以这样写 $origin = ['http://localhost:8080','http://localhost:8081']; $AllowOrigin = 'http://localhost:8080'; if(in_array($_SERVER["HTTP_ORIGIN"],$origin)) { $AllowOrigin = $_SERVER["HTTP_ORIGIN"]; } header("Access-Control-Allow-Origin: ".$AllowOrigin ); --------------------------------------------------------------------------------- //设置允许的请求方法,可以用*表示所有, header("Access-Control-Allow-Methods: POST"); --------------------------------------------------------------------------------- //如果允许请求携带cookie,此时 origin配置不能用 *,此时前端似乎也要做配置,让请求中携带cookie header('Access-Control-Allow-Credentials:true'); --------------------------------------------------------------------------------- //设置允许跨域的请求头,通常会在请求头里面加登录验证信息,那么服务端需要指定允许那些请求头,这里不能用*,多个字段用逗号隔开。 header('Access-Control-Allow-Headers:token');
The above is the detailed content of Can php access other web pages across domains?. For more information, please follow other related articles on the PHP Chinese website!