Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the http referer in laravel?

Tags:

I'm trying to get the Referer of my users. Like if they come from facebook, youtube, google or anything else.

Now I've tried something like that:

$referrer = $this->request->headers->get('referer');
$url = $referrer ? $this->to($referrer) : $this->getPreviousUrlFromSession();
return $url ?: $this->to('/'); // returns: Method referer does not exist.

This:

return $_SERVER["HTTP_REFERER"] // returns Undefined index: HTTP_REFERER

that:

session_start();
    
if ( !isset( $_SESSION["origURL"] ) ) {
    $_SESSION["origURL"] = $_SERVER["HTTP_REFERER"]; // returns Undefined index: HTTP_REFERER
}

But nothing worked like expected.

Does someone know a solution how I can check the referer?

I need that because I want to check if the user comes from some specific URL's and if so, I want to give the user some extra "clicks" to rank up. Something like a small affiliate system.

like image 224
PHPprogrammer42 Avatar asked Aug 16 '17 12:08

PHPprogrammer42


People also ask

How to get referer in Laravel?

$referrer = $this->request->headers->get('referer'); $url = $referrer ? $this->to($referrer) : $this->getPreviousUrlFromSession(); return $url ?: $this->to('/'); // returns: Method referer does not exist.

How do I get referer from HTTP request?

It's available in the HTTP referer header. You can get it in a servlet as follows: String referrer = request. getHeader("referer"); // Yes, with the legendary misspelling.

How do I get http referer in PHP?

$_SERVER['HTTP_REFERER'] will give you the referrer page's URL if there exists any. If users use a bookmark or directly visit your site by manually typing in the URL, http_referer will be empty. Also if the users are posting to your page programatically (CURL) then they're not obliged to set the http_referer as well.

What is $_ server [' Http_referer ']?

$_SERVER['HTTP_REFERER'] Returns the complete URL of the current page (not reliable because not all user-agents support it)


1 Answers

It seems like this will do what you are looking for :

Request::server('HTTP_REFERER').

You can read the Api DOC here :

https://laravel.com/api/8.x/Illuminate/Http/Request.html#method_server

like image 121
Thibault Dumas Avatar answered Sep 17 '22 13:09

Thibault Dumas