php apache開啟跨域模式過程詳解
更新時間:2019年07月08日 10:39:40 作者:hnlixf
這篇文章主要介紹了php apache開啟跨域模式過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
apaceh 配置:
<VirtualHost *:80>
ServerAdmin xxx@qq.com
DocumentRoot "C:/htdocs/demo"
ServerName dev.dd.cn
##ErrorLog "logs/dummy-host.localhost-error.log"
##CustomLog "logs/dummy-host.localhost-access.log" combined
<Directory "C:/htdocs/demo">
#Require all denied
Header set Access-Control-Allow-Origin *
</Directory>
</VirtualHost>
PHP文件設(shè)置:
<?php
header("Access-Control-Allow-Origin:*");
//處理請求輸出數(shù)據(jù)
?>
配置的含義是允許任何域發(fā)起的請求都可以獲取當(dāng)前服務(wù)器的數(shù)據(jù)。當(dāng)然,這樣有很大的危險性,惡意站點可能通過XSS攻擊我們的服務(wù)器。所以我們應(yīng)該盡量有針對性的對限制安全的來源,例如下面的設(shè)置使得只有http://feng.com這個域才能跨域訪問服務(wù)器的API。
httpd.conf:
<VirtualHost *:80>
ServerAdmin xxx@qq.com
DocumentRoot "C:/htdocs/demo"
ServerName dev.dd.cn
##ErrorLog "logs/dummy-host.localhost-error.log"
##CustomLog "logs/dummy-host.localhost-access.log" combined
<Directory "C:/htdocs/demo">
#Require all denied
Header set Access-Control-Allow-Origin http://feng.com
</Directory>
</VirtualHost>
PHP文件中:
header("Access-Control-Allow-Origin:http://feng.com");
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
php小經(jīng)驗:解析preg_match與preg_match_all 函數(shù)
本篇文章是對php中的preg_match函數(shù)與preg_match_all函數(shù)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06

