2017-11-20 5 views
3

アンプ形式用のCORSヘッダーに関するドキュメントは簡単にできますが、すべてを正しく行っても、私はまだ少し損失があります。PHPでamp-formの正しいヘッダを出力する方法は?

私のフォームは、自分のウェブサイトからも、GoogleのAMPの結果からも機能しているようです。しかし、私の開発ウェブサイトからはうまくいかない。私はそれが本当に非常に安全かどうかも分かりません。ここで私はこれは試行錯誤の多くの結果となっている、と私はドキュメントがはるかに可能性があることを考えて助けることができないhttps://podnews.net

に住んでスクリプトに、これまで使用しているコードがありますこの問題についてはっきりしています。

header('Cache-Control: private, no-cache'); 
header('Access-Control-Allow-Origin: '.$_SERVER['HTTP_ORIGIN']); 
header('Access-Control-Allow-Credentials: true'); 
header('access-control-expose-headers: AMP-Access-Control-Allow-Source-Origin'); 
header('AMP-Access-Control-Allow-Source-Origin: https://podnews.net'); 
header('Content-Type: application/json'); 

特に:私はそれを理解するよう$_SERVER['HTTP_ORIGIN']は、AMPのキャッシュを含めることができます。

ここで正しい有効な値は何ですか?複数の値を追加するにはどうすればよいですか(そこに少なくとも2つのAMPキャッシュがあるとします)。なぜそれが開発サイトで働いていないのですか?http://dev.podnews.netのようなものです(HTTPSではなくHTTPSであるというCORSのエラーです)。すべてのAMP開発者が簡単に参照できるように、これをどのように書くことができますか?

答えて

3

もっとたくさんあいた後、私は答えはここにかなり扱いにくいコードだと思う:

header('Cache-Control: private, no-cache'); 

$thisDomain="https://podnews.net"; // The main production domain 
$devDomain="http://dev.podnews.net"; // The development domain 

$googleAMPCacheSubdomain=str_replace(".","-",str_replace("-","--",$thisDomain)); 

//If you use an IDN, you've got more work to do in the above to work out your AMP cache subdomain 
//https://github.com/ampproject/amphtml/blob/master/spec/amp-cors-requests.md has details 

$validOrigins=array('https://'.$googleAMPCacheSubdomain.'.cdn.ampproject.org','https://cdn.ampproject.org','https://amp.cloudflare.com',$thisDomain,$devDomain); 

if (!in_array($_SERVER['HTTP_ORIGIN'],$validOrigins)) { 
    header('X-Debug: '.$_SERVER['HTTP_ORIGIN'].' is an unrecognised origin'); 
    header('HTTP/1.0 403 Forbidden');exit; 

    //Stop doing anything if this is an unfamiliar origin 
} 

if ($_GET['__amp_source_origin']!=$thisDomain AND $_GET['__amp_source_origin']!=$devDomain) { 
    header('X-Debug: '.$_GET['__amp_source_origin'].' is an unrecognised source origin'); 
    header('HTTP/1.0 403 Forbidden');exit; 

    //Stop doing anything if this is an unfamiliar source origin 
    //Note: if using Amazon Cloudfront, don't forget to allow query strings through 
} 

header('Access-Control-Allow-Origin: '.$_SERVER['HTTP_ORIGIN']); 
header('Access-Control-Allow-Credentials: true'); 
header('Access-Control-Expose-Headers: AMP-Access-Control-Allow-Source-Origin'); 
header('AMP-Access-Control-Allow-Source-Origin: '.urldecode($_GET['__amp_source_origin'])); 
header('Content-Type: application/json'); 
// You're in! 

私は、これは他の人が便利かもしれない素敵なコピー/貼り付け可能な答えであると思います。大変な作業です!

関連する問題