繁体   English   中英

使用PHP的CORS(跨源资源共享)

[英]CORS (cross origin resource sharing) using PHP

我只是遵循了CORS的PHP标头,在这里我有一个奇怪的行为,我在本地计算机上创建了两个简单的页面,一个页面(content.php)使用端口1112,另一个页面(sample.html)使用端口1113,我注意到了试图从content.php检索信息的奇怪行为sample.html,这是两个页面的代码sample.html

<!DOCTYPE html>
<html>
<head>
<title>Sample</title>
<script type="text/javascript">
    var xmlhttp = new XMLHttpRequest();
    function makerequest(serverPage, objID) {
        var obj = document.getElementById(objID);
        xmlhttp.open("POST", serverPage);
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                obj.innerHTML = xmlhttp.responseText;
            }
        }
        xmlhttp.send();
}
</script>
<body onload="makerequest ('http://localhost:1112/content.php','hw')">
<div align="center">
<h1>Sample</h1>
<div id="hw"></div>
</div>
</body>
</html>

和其他页面content.php

<!DOCTYPE HTML>
<html>
<head>
<?php
header("Access-Control-Allow-Origin: http://localhost:1113/sample.html");
?>
<title>page1</title>
</head>
<body>
<div style="width: 770px; text-align: left; color: green;">
<h1>Page 1</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod?
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, ?
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.?
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu ?
fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in?
culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
</body>
</html>

现在,当我尝试使用chrome&firefox运行sample.html时,即使我在标头中提供了原始链接,它也会被阻止;如果我使用IE运行它,则要使其正常工作,我需要给标头(*)加上不同从我想做的事情中,我试图找出为什么它无法按我想要的方式在chrome和firefox中工作

这是我从chrome XMLHttpRequest获取的错误消息,无法加载http:// localhost:1112 / content1.php “ Access-Control-Allow-Origin”标头的值“ http:// localhost:1113 / sample.html ”不等于提供的来源。 因此,不允许访问源' http:// localhost:1113 '。

对您的代码进行以下更改:

文件:content.php

<?php
//header("Access-Control-Allow-Origin: http://localhost:1113/sample.html");
header("Access-Control-Allow-Origin: http://localhost:1113");

?>

它应该工作。

与SOP强制执行的起源相同(并且CORS可以绕过)的定义是:“如果两个页面的协议,端口和主机相同,则两个页面具有相同的起源。”

在您的代码中,应该添加CORS标头以反映浏览器可以显示“ content.php”内容的原点, 因此您必须将“ origin”指定为标头值,即http :// localhost:1113 (不是' http:// localhost:1113 / sample.html ')

另外,“不幸的是,无论使用localhost是什么,我都相信您会遇到CORS问题,请考虑尝试使用本地IP。” 弗雷多的回答是不正确的。 在确定来源时,浏览器将整体处理[ scheme + host + port ]。 因此,使用具有不同端口号的localhost应该被视为不同的源,而不会出现任何问题。

尝试添加以下标头:

"Access-Control-Allow-Headers: Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With"

"Access-Control-Allow-Methods: GET, PUT, POST"

如果在httpd.conf或.htaccess中使用apache,则将其添加为服务器方法而不是通过PHP中的页面也可能会有所帮助。

此答案来自此博客文章(使用node.js / express) http://williambert.online/2013/06/allow-cors-with-localhost-in-chrome/

You may add following lines in  .htaccess code where you have palce sample.html

Header add Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type"
Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"

要么

 In sample.html file add below headers on top.

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM