laravel 如何从laravel HTTP请求类获取传入请求的协议(http/https)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42969285/
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
How to get protocol(http/https) of the incoming request from the laravel HTTP request class?
提问by Sathishkumar R
In my new application API, I have to check the request from the third party URL's should be in https. If it is not https, I have to return the message as "connection is not secure'. Can anyone help me?
在我的新应用程序 API 中,我必须检查来自第三方 URL 的请求是否应该在 https 中。如果不是 https,我必须返回消息“连接不安全”。有人可以帮助我吗?
回答by Daniel Tran
Here you are:
这个给你:
Determining If The Request Is Over HTTPS
if (Request::secure())
{
//
}
回答by Anar Bayramov
Daniel Tran's answer is correct just for information HTTPS type requests have an extra field HTTPSon requests. Sometimes this field can be equal to offtoo but nothing else.
Daniel Tran 的回答仅适用于 HTTPS 类型请求在请求上有一个额外字段HTTPS 的信息。有时这个字段也可以等于off但没有别的。
so you can just write a code like
所以你可以写一个像
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') {
doSomething();
}
Laravel Request Class also inherited something totally similar from symphony. Which You can find under;
Laravel Request Class 也继承了与 Symphony 完全相似的东西。你可以在下面找到;
vendor/symfony/http-foundatiton/Request.php
供应商/symfony/http-foundatiton/Request.php
public function isSecure()
{
if ($this->isFromTrustedProxy() && self::$trustedHeaders[self::HEADER_CLIENT_PROTO] && $proto = $this->headers->get(self::$trustedHeaders[self::HEADER_CLIENT_PROTO])) {
return in_array(strtolower(current(explode(',', $proto))), array('https', 'on', 'ssl', '1'));
}
$https = $this->server->get('HTTPS');
return !empty($https) && 'off' !== strtolower($https);
}