用php解决跨域的问题

解决跨域问题一般是在nginx加入Access-Control-Allow-Origin: * 来处理,但chrome等浏览器不允许星号的存在,只能设置一个域名,这样我们很多时候就不方便.

 

这里给出一个php的解决方式,把下面代码直接放到项目的index.php中即可.

$origin = isset($_SERVER[‘HTTP_ORIGIN’])?$_SERVER[‘HTTP_ORIGIN’]:””;  //获取请求来源,这一行需要根据情况来获取,万能的程序员自行检查吧
if($origin){
    header(“Access-Control-Allow-Origin: $origin”);
    header(‘Access-Control-Allow-Credentials:true’);
    header(‘Access-Control-Allow-Methods:GET, POST, OPTIONS’);
    header(‘Access-Control-Allow-Headers:DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type’);
}


( ! ) Warning: Missing argument 1 for cwppos_show_review(), called in /data/www/enjoyphp/wp-content/themes/flat/content-single.php on line 29 and defined in /data/www/enjoyphp/wp-content/plugins/wp-product-review/includes/legacy.php on line 18
Call Stack
#TimeMemoryFunctionLocation
10.0000355304{main}( ).../index.php:0
20.0001355592require( '/data/www/enjoyphp/wp-blog-header.php' ).../index.php:17
30.179210154896require_once( '/data/www/enjoyphp/wp-includes/template-loader.php' ).../wp-blog-header.php:19
40.180510226040include( '/data/www/enjoyphp/wp-content/themes/flat/single.php' ).../template-loader.php:74
50.267110780616get_template_part( ).../single.php:5
60.267110781136locate_template( ).../general-template.php:167
70.267210781232load_template( ).../template.php:643
80.267210781616require( '/data/www/enjoyphp/wp-content/themes/flat/content-single.php' ).../template.php:686
90.272810766432cwppos_show_review( ).../content-single.php:29

发表评论

电子邮件地址不会被公开。 必填项已用*标注