How to Get Headers From Request in Laravel ?

Share Me
  •  
  •  
  •  
  • 2
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    2
    Shares
How to Get Headers From Request in Laravel

Now today in this blog ,i will give you a example how to get Get headers from request it also can get headers from in this example. So we will get three lots method to get a header from request. Now we will see how to get headers From Request following by example. So here we see How to Get Headers From Request in Laravel.

Now I will give a example 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

Now 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

Now 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 "
]

Read Also : How to get all columns from table in Laravel?

Thanks for read. I hope it help you. For more you can follow us onĀ facebook.

About Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Shahriar Sagor →