Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get protocol(http/https) of the incoming request from the laravel HTTP request class?

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?

like image 339
Sathishkumar R Avatar asked Mar 23 '17 07:03

Sathishkumar R


1 Answers

Daniel Tran's answer is correct just for information HTTPS type requests have an extra field HTTPS on requests. Sometimes this field can be equal to off too but nothing else.

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;

vendor/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);
    }
like image 146
Anar Bayramov Avatar answered Oct 13 '22 02:10

Anar Bayramov