How to Get Request Referer Path

How to get full path of referer in Ruby?

To get full path of request URI use REQUEST_URI

request.env['REQUEST_URI']

To get full path of the referer use HTTP_REFERER

request.env['HTTP_REFERER']

The complete list of available options in request.env hash are

"GATEWAY_INTERFACE"=>"CGI/1.1", 
"PATH_INFO"=>"/accounts/1/users/new",
"QUERY_STRING"=>"",
"REMOTE_ADDR"=>"127.0.0.1",
"REMOTE_HOST"=>"abcd.com",
"REQUEST_METHOD"=>"GET",
"REQUEST_URI"=>"http://localhost:3000/accounts/1/users/new",
"SCRIPT_NAME"=>"",
"SERVER_NAME"=>"localhost",
"SERVER_PORT"=>"3000",
"SERVER_PROTOCOL"=>"HTTP/1.1",
"SERVER_SOFTWARE"=>"WEBrick/1.3.1 (Ruby/1.9.3/2014-11-13)",
"HTTP_HOST"=>"localhost:3000",
"HTTP_CONNECTION"=>"keep-alive",
"HTTP_UPGRADE_INSECURE_REQUESTS"=>"1",
"HTTP_USER_AGENT"=>"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36",
"HTTP_ACCEPT"=>"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
"HTTP_REFERER"=>"http://localhost:3000/accounts/1/users",
"HTTP_ACCEPT_ENCODING"=>"gzip, deflate, br",
"HTTP_ACCEPT_LANGUAGE"=>"en-US,en;q=0.9,hi;q=0.8",
"HTTP_VERSION"=>"HTTP/1.1",
"REQUEST_PATH"=>"/accounts/1/users/new",
"ORIGINAL_FULLPATH"=>"/accounts/1/users/new"

You can concatenate string to get the desired result from above available values in env hash.

Checking the referer of 'path/:id'

This will work, however is far from being pretty. It is however quite flexible and is relaying on generated url helper, so shouldn't break if you decide to change url mappings.

if URI(request.referer).path =~ Regexp.new(customer_path(':customer_id').gsub(':customer_id', '\d+'))

MUCH, MUCH, BETTER SOLUTION:

Rails application have a method to recognize paths already and returns with controller/action:

Rails.application.routes.recognize_path(URI(request.referer).path)
#=> {:controller => 'customers', :action => 'show', :id => '88'}

You can use it to write a helper method:

def is_referer_customer_show_action?
referer_url = Rails.application.routes.recognize_path(URI(request.referer).path)
referer_url[:controller] == 'customers' && referer_url[:action] == 'show'
end

How to get the referer url of a previous page in Rails 4

What happens is if the user is on /visitors/owner-faq page and clicks on the sign up link, the referrer in the sign up page is /visitors/owner-faq. This is the reason why url_for(:back) works on that page. Now, after submitting the form in the sign up page, the referrer is now changed to the sign up url, which makes sense.

What you want to do is save the referrer in the session when the user visits the sign up page. So in the action that serves the sign up page (say that's the users new action)

class UsersController < ApplicationController
def new
session[:referrer] = request.referrer
end
end

So when the user submits the sign up form, you can access the session

def create
referrer = session.delete(:referrer)
user = User.new
user.role = 'owner' if referrer == '/visitors/owner-faq'
user.save!
end

how do you determine if request.referrer matches a Rails restful path?

You could extract the path portion of the request.referer using the following:

URI(request.referer).path

You can then use Rails.application.routes.recognize_path to check if path maps to a controller and action, e.g.:

my_path = URI(request.referer).path 
# => /books

Rails.application.routes.recognize_path(my_path)
# => {:action=>"show", :controller=>"books", :page=>"4"}

How to extract scheme, host, path, and query string from the HTTP Referer using C# ASP.NET Core

if you save the referrer as a string say refURL = Context.Request.Headers["Referer"].ToString()

Then

var address = new System.Uri(refURL);

var scheme = address.Scheme ;
var host = address.Host;

etc

details on Uri Class

Getting 'path' of previous URL from template

Looks like there isn't a direct way to do that in the template, so this is what I ended up doing in the template to check if a particular (relative) url is part of the previous (absolute) URL :

{% url 'schoollist:add_school' as add_school_url %}
{% if add_school_url in request.META.HTTP_REFERER %}
Thanks for adding!
{% endif %}

Not getting full path using req.get('Referrer') if in https

This is new, but is now the expected behavior. I assume localhost gets special treatment, which is why it works locally.

As of Chrome 85 (Aug 2020), the default referrer policy is strict-origin-when-cross-origin, which means that the Referer (sic) header will only contain the referring site's Origin (ie, no path) when cross-domain requests are made.

If you want the full path of your page sent to other origins, the referring page must have been sent with a Referrer-Policy header or include referrerpolicy attributes in HTML.

In other words, your tracking pixel should look like this:

<img src="https://example.com/p" referrerpolicy="no-referrer-when-downgrade" height="1" width="1">


Related Topics



Leave a reply



Submit