php 如何在php中获取http请求源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25520085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:52:16  来源:igfitidea点击:

How to get http request origin in php

phpapachehttppostget

提问by m_junior

I want to create an API, and to authenticate API consumers, I will provide an API KEY, App-id and App-Secret. The problem is that I want to know where the http Request is coming from, so that I can know if the Host that is making que request is the registered Host. For example : www.someone.com has an app-id :0001, app-secret:1200 and api-key:458. If this credentials are used to make A request, I want to know if the requester is really www.someone.com

我想创建一个 API,为了验证 API 消费者,我将提供一个 API KEY、App-id 和 App-Secret。问题是我想知道http请求是从哪里来的,这样我就可以知道发出que请求的主机是否是注册的主机。例如:www.someone.com 有一个 app-id:0001、app-secret:1200 和 api-key:458。如果这个凭证是用来做A请求的,我想知道这个请求者是不是真的www.someone.com

采纳答案by Sunit

Use $_SERVER['HTTP_REFERER']. It is the address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERERas a feature.

使用$_SERVER['HTTP_REFERER']. 它是将用户代理引用到当前页面的页面地址(如果有的话)。这是由用户代理设置的。不是所有的用户代理都会设置这个,有些提供修改HTTP_REFERER功能的能力。

For further restrictions you can perform the following. example.comshould be changed to your domain.

对于进一步的限制,您可以执行以下操作。example.com应该更改为您的域。

IIS set below in web config:

IIS 在 web 配置中设置如下

add name="Access-Control-Allow-Origin" value="http://www.example.com"

Apache set below in httpd.conf/apache.conf

Apache 在 httpd.conf/apache.conf 中设置如下

Header add Access-Control-Allow-Origin "http://www.example.com"

回答by hex494D49

Generally, this header should do the job. Having the domain name in this header

通常,这个标题应该可以完成这项工作。在此标头中包含域名

header("Access-Control-Allow-Origin: " . $_SERVER['HTTP_ORIGIN'] . "");
// use domain name instead of $_SERVER['HTTP_ORIGIN'] above

but if you want to check for more info, use something like the following snippet

但如果你想检查更多信息,请使用类似以下代码段的内容

$allowed = array('domain1', 'domain2', 'domain3'); 

if(isset($_SERVER['HTTP_ORIGIN']) && in_array($_SERVER['HTTP_ORIGIN'], $allowed)){
    // SELECT credentials for this user account from database
    if(isset($_GET['api_key'], $_GET['app_secret'])
        && $_GET['api_key'] == 'api_key_from_db' 
        && $_GET['app_secret'] == 'app_secret_from_db'
    ){
        // all fine
    }else{
        // not allowed
    }
}else{
    // not allowed
}

If the users have to pass more data to your service, use POSTinstead of GET

如果用户必须将更多数据传递给您的服务,请使用POST而不是GET

回答by Kamil Kie?czewski

Laravel 5: in request method controller:

Laravel 5:在请求方法控制器中:

$origin = request()->headers->get('origin');

回答by Peter

Using a var_dumpyou can see all that the requesthas to offer.

使用 avar_dump您可以看到所request提供的所有内容。

var_dump($_REQUEST);

Do a var_dumpon the serverglobal as well. It contains alot of usefull information.

var_dumpserver全球范围内也做一个。它包含很多有用的信息。

var_dump($_SERVER);

回答by sensadrome

I think what you mean is that you want to access the "Origin" header in the request headers (as opposed to setting it in the response headers).

我认为您的意思是您想访问请求标头中的“Origin”标头(而不是在响应标头中设置它)。

For this the easiest way is to access the built in getallheaders()function - which is an alias for apache_request_headers() - N.B. this is assuming you are using php as a module.

为此,最简单的方法是访问内置的getallheaders()函数 - 它是apache_request_headers()的别名- 注意,这是假设您使用 php 作为模块。

This returns an array so the Origin header should be available like this:

这将返回一个数组,因此 Origin 标头应该像这样可用:

$request_headers = getallheaders();
$origin = $request_headers['Origin'];

If you are using php via something like fastcgi then I believe it would be made available in the environment - usually capitalised and prefixed by "HTTP_" so it should be $_SERVER['HTTP_ORIGIN'].

如果您通过诸如 fastcgi 之类的东西使用 php,那么我相信它会在环境中可用 - 通常大写并以“HTTP_”为前缀,所以它应该是$_SERVER['HTTP_ORIGIN'].

Hope that helps anyone else looking for this :)

希望能帮助其他人寻找这个:)

回答by Ar0010r

in laravel 7 this worked for me

在 laravel 7 这对我有用

request()->headers->get('referer');

request()->headers->get('referer');