Is there a way to find out if a page request came from a single application?

In Rails3, is there a way to check if the page I'm currently viewing was being requested from the same application without using a hard-coded 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 it does not check "www". in front of the domain and all other possible situations.

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

+4
source share
3 answers

I think you are looking at it wrong.

If you look on the Internet, find a site with a search function and follow the link, you will see a parameter showing what you were looking for.

This is a good way to do it.

Doing this HTTP_REFERER seems a little fragile and will not work, for example, from a bookmark or hosted link.

eg.

 /cars/12?from_search=sports+cars 

then you can just look params[:from_search]

If you really need to do this with HTTP_REFERER , you probably won't 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 
+10
source

Try something like this:

 ref = URI.parse(controller.request.env["HTTP_REFERER"]) if ref.host == ENV["HOSTNAME"] # do something 

To try to get the controller / action from the link page:

 ActionController::Routing::Routes.recognize_path(url.path) #=> {:controller => "foo", :action => "bar"} 
+2
source

Create an internal_request? method internal_request? using request.referrer .

Compare host and port with request.referrer with your host and port application.

 require 'uri' # Might be necesseary. def internal_request? return false if request.referrer.blank? referrer = URI.parse( request.referrer ) application_host = Rails.application.config.action_mailer.default_url_options[ :host ] application_port = Rails.application.config.action_mailer.default_url_options[ :port ] return true if referrer.host == application_host && referrer.port == application_port false end 

And then name it as you need, most likely in application_controller.rb :

 if internal_request? do_something end 

Some reservations:

  • This may need to be changed if you are using subdomains. Easy, however.
  • This will require setting your host and port for ActionMailer in your configuration, which is shared.
  • You might want to do this the other way around, for example external_request? , since you are likely to deal with these situations clearly. This will allow you to do something like this:

     do_something_unique if external_request? 
0
source

All Articles