Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to know if a page request came from the same application?

In Rails3, is there a way to check if the page I'm rendering now was requested from the same application, without the use of the hardcoded domain name?

I currently have:

def back_link(car_id = '')
  # Check if search exists
  uri_obj = URI.parse(controller.request.env["HTTP_REFERER"]) if controller.request.env["HTTP_REFERER"].present?
  if uri_obj.present? && ["my_domain.com", "localhost"].include?(uri_obj.host) && uri_obj.query.present? && uri_obj.query.include?('search')
    link_to '◀ '.html_safe + t('back_to_search'), url_for(:back) + (car_id.present? ? '#' + car_id.to_s : ''), :class => 'button grey back'
  end
end

But this doesn't check for the "www." in front of the domain and all other possible situations.

It would also be nice if I could find out the specific controller and action that were used in the previous page (the referrer).

like image 394
Cristian Avatar asked Oct 25 '11 14:10

Cristian


People also ask

How can I tell where a HTTP request came from?

There is absolutely no way to know with certainty if a request came from a browser or something else making an HTTP request. The HTTP protocol allows for the client to set the User Agent arbitrarily.

How do you check if a request if coming from the same server or different server?

Basically : you cannot. With the HTTP protocol, each request is independent from the others. A first idea would be to check the Referer HTTP header, but note that : It can be faked (it's sent by the browser)

Are all browser requests get requests?

Yes. By default, if you type in address bar or click any link, then it will be a GET call.


2 Answers

I think you're looking at this the wrong way.

If you look around the web, find a site with a search feature, and follow the link you'll see a param showing what was searched for.

That's a good way to do it.

Doing it by HTTP_REFERER seems a bit fragile, and won't work, for example, from a bookmark, or posted link.

eg.

/cars/12?from_search=sports+cars

then you can just look up the params[:from_search]

If you really need to do it by HTTP_REFERER then you probably dont have to worry about subdomains. Just;

def http_referer_uri
  request.env["HTTP_REFERER"] && URI.parse(request.env["HTTP_REFERER"])
end

def refered_from_our_site?
  if uri = http_referer_uri
    uri.host == request.host
  end
end

def refered_from_a_search?
  if refered_from_our_site?
    http_referer_uri.try(:query)['search']
  end
end
like image 141
Matthew Rudy Avatar answered Nov 15 '22 15:11

Matthew Rudy


Try something like this:

ref = URI.parse(controller.request.env["HTTP_REFERER"])

if ref.host == ENV["HOSTNAME"]
  # do something

To try and get the controller/action from the referring page:

ActionController::Routing::Routes.recognize_path(url.path)
  #=> {:controller => "foo", :action => "bar"}
like image 41
Alex Peattie Avatar answered Nov 15 '22 17:11

Alex Peattie