10

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!

3
  • Please show us the dump of your $request and a picture of the headers being sent with your Web Inspector. Commented Dec 13, 2016 at 10:02
  • @Ohgodwy Updated the question with photos Commented Dec 13, 2016 at 10:06
  • HTTP Headers that contain underscore (_) are not transferred to the server if you use Nginx, unless not explicitly set underscores_in_headers on; Commented Jul 29, 2020 at 15:22

3 Answers 3

12

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

Sign up to request clarification or add additional context in comments.

4 Comments

no, I'm willing to get the exact header from the request, it's not a parameter
If request()->header('accessing_from'); doesn't show anything, you may want to read daver answer here.
Trying to access the header with request()->header('accessing_from') returns a empty array :(
Need to use Accessing-From - updated answer stackoverflow.com/a/59427256/372215
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');

1 Comment

@HRLET, already tried that before, unfortunately none of both methods works.
1

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');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.