8

I'm trying to access a custom header from the Request in Laravel. The header name is "accessing_from". Listing all the headers in Laravel, gives me only the "standard ones", but the one that I've set isn't present in the list. Checking in the browser network tab I can see that the header gets sent. So I'm wondering how to access it from within Laravel.

I'm using Angular2 to make the request with the default http service.

The Laravel's $response->header() dump:

enter image description here

The web inspector's log:

enter image description here Thanks to anyone!

Community
  • 1
  • 1
Caius
  • 2,084
  • 6
  • 31
  • 47

3 Answers3

9

Are you talking about get parameter or something? If so, use:

request()->accessing_from;

For header you should use:

request()->header('accessing_from');

The working solution for this was the answer (the last one) of daver here: Laravel get request header

Community
  • 1
  • 1
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • no, I'm willing to get the exact header from the request, it's not a parameter – Caius Dec 13 '16 at 10:05
  • 1
    If `request()->header('accessing_from');` doesn't show anything, you may want to read `daver` answer [here](http://stackoverflow.com/questions/20853604/laravel-get-request-headers). – Alexey Mezenin Dec 13 '16 at 10:11
  • Trying to access the header with request()->header('accessing_from') returns a empty array :( – Caius Dec 13 '16 at 10:11
  • Need to use `Accessing-From` - updated answer https://stackoverflow.com/a/59427256/372215 – Artistan Dec 20 '19 at 15:01
2

Have you tried simple php?

<?php
// Replace XXXXXX_XXXX with the name of the header you need in UPPERCASE
$headerStringValue = $_SERVER['HTTP_XXXXXX_XXXX'];

Full answer: https://stackoverflow.com/a/541463/3548658

The docs says: https://laravel.com/api/5.3/Illuminate/Http/Request.html#method_header

use Request;
Request::header('accessing_from');
Community
  • 1
  • 1
HARLET
  • 31
  • 3
0

getting a custom header in Laravel 5.8.

this should apply to earlier versions as well.

If using a header like X-Requested-With: XMLHttpRequest you may notice that it converts this to HTTP_X_REQUESTED_WITH.

This, in turn, is converted to lower case version for the header() method.

request()->header('x_requested_with');

I would suggest using Accessing-From: admin which will add the apache header HTTP_ACCESSING_FROM. And you will be able to access it via the header function like this...

request()->header('accessing_from');
Artistan
  • 1,982
  • 22
  • 33