简体   繁体   English

如何在php中获取http请求源

[英]How to get http request origin in php

I want to create an API, and to authenticate API consumers, I will provide an API KEY, App-id and App-Secret.我想创建一个 API,为了验证 API 消费者,我将提供一个 API KEY、App-id 和 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.问题是我想知道http请求是从哪里来的,这样我就可以知道发出que请求的主机是否是注册的主机。 For example : www.someone.com has an app-id :0001, app-secret:1200 and api-key:458.例如:www.someone.com 有一个 app-id:0001、app-secret:1200 和 api-key:458。 If this credentials are used to make A request, I want to know if the requester is really www.someone.com如果这个凭证是用来做A请求的,我想知道这个请求者是不是真的www.someone.com

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 POST instead of GET如果用户必须向您的服务传递更多数据,请使用POST而不是GET

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

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

Use $_SERVER['HTTP_REFERER'] .使用$_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_REFERER as a feature.并不是所有的用户代理都会设置这个,有些提供修改HTTP_REFERER的能力作为一个特性。

For further restrictions you can perform the following.对于进一步的限制,您可以执行以下操作。 example.com should 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"

Technically neither origin nor referer are required HTTP headers, all of these answers are based on specific browser headers sent, and basing your system on different behaviors of clients is a bad idea.从技术上讲,无论是origin还是referer都不需要 HTTP 标头,所有这些答案都基于发送的特定浏览器标头,并且根据客户端的不同行为建立系统是一个坏主意。

The correct answer is you can't reliably get the client origin on every request because it isn't required as part of the HTTP specification.正确答案是您无法可靠地获取每个请求的客户端来源,因为它不是 HTTP 规范的一部分。

Using a var_dump you can see all that the request has to offer.使用var_dump您可以看到request必须提供的所有内容。

var_dump($_REQUEST);

Do a var_dump on the server global as well.也在server全局上做一个var_dump It contains alot of usefull information.它包含很多有用的信息。

var_dump($_SERVER);

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 () - NB 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 :)希望能帮助其他人寻找这个:)

in laravel 7 this worked for me在 laravel 7 这对我有用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM