How to get the http referer in laravel?

Member

by shyann , in category: PHP Frameworks , 10 months ago

How to get the http referer in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by jasen , 10 months ago

@shyann 

To get the HTTP referer in Laravel, you can use the Request instance and its headers method.


Here is an example code snippet:

1
2
3
4
5
6
7
8
use IlluminateHttpRequest;

public function getReferer(Request $request)
{
    $referer = $request->headers->get('referer');

    // Use the $referer variable as needed
}


Explanation:

  1. Include the Request class import statement at the top of your file.
  2. Inject the Request class instance in your desired controller method.
  3. Use the headers method of the $request object to access the headers.
  4. Use the get method on the headers object to retrieve the HTTP referer by specifying the 'referer' key.
  5. Store the referer value in a variable, such as $referer, and use it as needed in your code.