【问题标题】:PHP AMP form CORS IssuePHP AMP表单CORS问题
【发布时间】:2020-01-31 12:44:27
【问题描述】:

页面在单域/相同域上运行良好,但在多域/跨域的情况下会产生问题。

表单页面

https://example.com

ampform.html

<!doctype html>
<html amp>
<head>
<meta charset="utf-8">
<title>amp-form</title>
<script async src="https://cdn.ampproject.org/v0.js"></script>
<script async custom-element="amp-form" src="https://cdn.ampproject.org/v0/amp-form-0.1.js"></script>
<script async custom-template="amp-mustache" src="https://cdn.ampproject.org/v0/amp-mustache-0.2.js"></script>
<link rel="canonical" href="https://amp.dev/documentation/examples/components/amp-form/index.html">
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
</head>
<body>
<h2 class="sample-heading">AMP Form Submission</h2>
<form target="_top" method="post" name="apiForm" id="apiForm" action-xhr="https://different.com/ampl/amp_thankyou.php"  
custom-validation-reporting="show-all-on-submit">
   <input type="text"
    id="as-you-go-name"
    name="name"
    placeholder="Name..."
    required>
  <span visible-when-invalid="valueMissing"
    validation-for="as-you-go-name"></span>
  <input type="submit" name="submitlogin" value="Submit" />
</form>
</body>
</html>

感谢页面

https://different.com

amp_thankyou.php

if(!empty($_POST)){
        $domain_url = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]";
        header("Content-type: application/json");
        header("Access-Control-Allow-Credentials: true");
        header("Access-Control-Allow-Origin: *");
        header("Access-Control-Allow-Origin: ". str_replace('.', '-','https://www.example.com') .".cdn.ampproject.org");
        header("AMP-Access-Control-Allow-Source-Origin: " . $domain_url);
        header("Access-Control-Expose-Headers: AMP-Access-Control-Allow-Source-Origin");
        header("AMP-Redirect-To: https://www.different.com/amp_thankyou.php");
        header("Access-Control-Expose-Headers: AMP-Redirect-To, AMP-Access-Control-Allow-Source-Origin"); 
        echo json_encode(array('successmsg'=>'data post'));
        exit;
}

【问题讨论】:

  • ...如果出现多域创建问题...不清楚您要问什么
  • 表示一个域中的表单页面和另一个域中的操作页面。从不同域调用操作页面
  • 请不要只是在这里转储代码 - 问一个实际问题。你到底有什么问题?错误消息清楚地表明响应中没有Access-Control-Allow-Origin - 那么到目前为止您实际上做了什么来尝试调试它?
  • 其实我已经添加了上面的代码来避免 CORS 问题。仍然收到山姆错误。我错过了什么

标签: php cors


【解决方案1】:

以下更改对我有用。

.htaccess

SetEnvIf Origin "^http(s)?://(.+\.)?(www\.example.com|www\.different.com)$" origin_is=$0 
Header always set Access-Control-Allow-Origin %{origin_is}e env=origin_is

感谢页面

https://different.com

amp_thankyou.php

 header("Content-type: application/json");
    header("Access-Control-Allow-Credentials: true");
    $valid_cors = array("www.example.com","www.different.com");
    if(in_array($_SERVER['HTTP_ORIGIN'],"https://".$valid_cors)) {
        header('Access-Control-Allow-Origin: https://{$valid_cors}', false);
        header("Access-Control-Allow-Methods: GET, POST, PUT");
        header("Access-Control-Allow-Headers: Content-Type");
    }

    header('AMP-Access-Control-Allow-Source-Origin: '.'http://'. $_SERVER['HTTP_HOST']);
    header("AMP-Redirect-To: https://www.different.com/amp_thankyou.php");
    header("Access-Control-Expose-Headers: AMP-Access-Control-Allow-Source-Origin, AMP-Redirect-To");

【讨论】:

    【解决方案2】:

    这是一个安全风险,但把它放在你的 php 文件的顶部

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

    【讨论】:

    • 我的错,我看到了两次。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-26
    • 2015-02-23
    • 2019-02-18
    • 2019-08-18
    • 2019-02-04
    • 2021-09-02
    相关资源
    最近更新 更多