weixin_33725239 2018-07-21 09:01 采纳率: 0%
浏览 39

Apache / PHP和动态CORS

I'm having a hard time to get around that CORS thing.

I have a javascript sending AJAX Put/Fetch requests to Apache/PHP script.

In this case, for the example, the javascript is running on CodePen, and the Apache/PHP is on a local server.

I'm checking the origin against a list of allowed hosts.

It should be possible to let PHP return headers like:

$headers = getallheaders();

if ( checkorigin($headers['Origin']) === false) $headers['Origin'] = null;

header('Access-Control-Allow-Origin: ' . $headers['Origin']);
header('Access-Control-Allow-Methods: PUT, POST');
header('Access-Control-Allow-Headers: content-type');
header('Access-Control-Allow-Credentials: true');

This doesn't work.

Hard coding https://s.codepen.io into the header does work.

Ideas anyone ?

Solution

Changing from:

header('Access-Control-Allow-Origin: ' . $headers['Origin']);

to:

header('Access-Control-Allow-Origin: ' . "{$_SERVER['HTTP_ORIGIN']}");

did the trick. Thanks to Rohit.007

  • 写回答

1条回答 默认 最新

  • 10.24 2018-07-21 10:09
    关注

    try

    header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");

    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记