Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP AMP表格CORS问题_Php_Cors - Fatal编程技术网

PHP AMP表格CORS问题

PHP AMP表格CORS问题,php,cors,Php,Cors,页面在单域/同一域上工作正常,但在多域/跨域的情况下,它会产生问题。 表格页 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> <s

页面在单域/同一域上工作正常,但在多域/跨域的情况下,它会产生问题。

表格页

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>

这是一个安全风险,但请将其放在php文件的顶部

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

下面的更改对我有用

.htaccess

谢谢你的页面

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;
}
 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");

…如果出现多域创建问题。。。不清楚您试图询问的是一个域中的表单页面和另一个域中的操作页面。从不同的域调用操作页面请不要在此处转储代码-询问实际问题。你到底有什么问题?错误消息清楚地表明响应中没有访问控制Allow Origin-那么到目前为止,您到底做了什么来尝试和调试它呢?实际上,我添加了上面的代码以避免CORS问题。仍然得到山姆的错误。我是不是错过了什么不好的东西,哪怕是两次。
 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");