Laravel Get Headers From Request Example

laravel get headers from request

Hi Dev,

In this blog ,I Will learn you how to get header in laravel. I will show example of laravel get headers from request you can easliy laravel get all headers from request. I will explain laravel get all headers.

I can blow you can easily get headers from request in laravel this example.

Exmaple:1

Here In this exmaple laravel Get headers from request to a header method

/**
 * Show the application dashboard.
 *
 * @return \Illuminate\Contracts\Support\Renderable
 */
public function index(Request $request)
{
    $headers = $request->header('connection');

    dd($headers);
}
Output :
"keep-alive"
Exmaple:2

Here In this exmaple laravel get headers from request using a request builder to a header method

/**
 * Show the application dashboard.
 *
 * @return \Illuminate\Contracts\Support\Renderable
 */
public function index()
{
    $headers = \Request::header();

   dd($headers);
}
Output :
array:13 [
  "host" => array:1 [
    0 => "localhost:8000"
  ]
  "connection" => array:1 [
    0 => "keep-alive"
  ]
  "cache-control" => array:1 [
    0 => "max-age=0"
  ]
  "upgrade-insecure-requests" => array:1 [
    0 => "1"
  ]
  "user-agent" => array:1 [
    0 => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"
  ]
  "sec-fetch-user" => array:1 [
    0 => "?1"
  ]
  "accept" => array:1 []
  "sec-fetch-site" => array:1 [
    0 => "same-origin"
  ]
  "sec-fetch-mode" => array:1 [
    0 => "navigate"
  ]
  "referer" => array:1 []
  "accept-encoding" => array:1 [
    0 => "gzip, deflate, br"
  ]
  "accept-language" => array:1 [
    0 => "en-US,en;q=0.9,la;q=0.8,gu;q=0.7"
  ]
  "cookie" => array:1 [
    0 => "_ga=GA1.1.2099257977.1573906086; __gads=Test; _gid=GA1.1.879789242.1575463476; __atuvc=13%7C46%2C68%7C47%2C5%7C48%2C17%7C49; XSRF-TOKEN=eyJpdiI6IlRZVE1VUnh2WFNu "
  ]
]
Exmaple:3

Here In this exmaple laravel Get headers from request to a getallheaders method

/**
 * Show the application dashboard.
 *
 * @return \Illuminate\Contracts\Support\Renderable
 */
public function index()
{
   $headers = getallheaders();

   dd($headers);
}
Output :
array:13 [
  "Host" => "localhost:8000"
  "Connection" => "keep-alive"
  "Cache-Control" => "max-age=0"
  "Upgrade-Insecure-Requests" => "1"
  "User-Agent" => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"
  "Sec-Fetch-User" => "?1"
  "Accept" => "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3"
  "Sec-Fetch-Site" => "same-origin"
  "Sec-Fetch-Mode" => "navigate"
  "Referer" => "http://localhost:8000/login"
  "Accept-Encoding" => "gzip, deflate, br"
  "Accept-Language" => "en-US,en;q=0.9,la;q=0.8,gu;q=0.7"
  "Cookie" => "_ga=GA1.1.2099257977.1573906086; __gads=Test; _gid=GA1.1.879789242.1575463476; __atuvc=13%7C46%2C68%7C47%2C5%7C48%2C17%7C49; XSRF-TOKEN=eyJpdiI6IlRZVE1VUnh2WFNu "
]
Exmaple:4

Here In this exmaple laravel Get headers from request to a apache_request_headers method

/**
 * Show the application dashboard.
 *
 * @return \Illuminate\Contracts\Support\Renderable
 */
public function index()
{
   $headers = apache_request_headers();

   dd($headers);
}
Output :
array:13 [
  "Host" => "localhost:8000"
  "Connection" => "keep-alive"
  "Cache-Control" => "max-age=0"
  "Upgrade-Insecure-Requests" => "1"
  "User-Agent" => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"
  "Sec-Fetch-User" => "?1"
  "Accept" => "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3"
  "Sec-Fetch-Site" => "same-origin"
  "Sec-Fetch-Mode" => "navigate"
  "Referer" => "http://localhost:8000/login"
  "Accept-Encoding" => "gzip, deflate, br"
  "Accept-Language" => "en-US,en;q=0.9,la;q=0.8,gu;q=0.7"
  "Cookie" => "_ga=GA1.1.2099257977.1573906086; __gads=Test; _gid=GA1.1.879789242.1575463476; __atuvc=13%7C46%2C68%7C47%2C5%7C48%2C17%7C49; XSRF-TOKEN=eyJpdiI6IlRZVE1VUnh2WFNu "
]

it will help you....