Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save http referer in rails

I'm trying to save the site that a user came from when they sign up. Right now I have a before_filter in my ApplicationController:

before_filter :save_referer

  def save_referer
    unless is_logged_in?
      session['referer'] = request.env["HTTP_REFERER"] unless session['referer']
    end
  end

Then when a user is created, it checks this session variable and sets it to nil. Sometimes this does not work and I'm worried there might be some unintended things happening with using session like this. Does anyone have a better way? Or some input perhaps?

EDIT: This is the logic I am using to save the referer:

def create     
    @user = User.new(params[:user])  
    if @user.save_with(session[:referer])
    ....
end

User

def save_with(referer)
    self.referer = referer unless referer == "null"
    self.save   
end

Is there any reason why this should not work?

like image 948
TenJack Avatar asked Feb 26 '10 13:02

TenJack


People also ask

What is request referer in rails?

request.referer gives you the previous URL or / if none. It is usually used to redirect the user back to the previous page (link)

How do I get the previous URL in Ruby on Rails?

In a web application there is no such thing as a previous url. The http protocol is stateless, so each request is independent of each other. You could have the Javascript code, that sends a request back, send the current url with the request.

How does HTTP referrer work?

The Referer HTTP request header contains an absolute or partial address of the page that makes the request. The Referer header allows a server to identify a page where people are visiting it from. This data can be used for analytics, logging, optimized caching, and more.

What is referer URL?

The address of the webpage where a person clicked a link that sent them to your page. The referrer is the webpage that sends visitors to your site using a link. In other words, it's the webpage that a person was on right before they landed on your page.


2 Answers

I think there's a flaw in your approach. As long as the user is hitting pages and is not logged in, the filter code will run. So the only way session['referer'] will not be nil is if they go straight to the signup page where they (presumably) post their login info and you check the session var.

I think you probably need to only check the referer once - to do this you'll have to modify your filter code.

def save_referer
  unless is_logged_in?
    unless session['referer']
      session['referer'] = request.env["HTTP_REFERER"] || 'none'
    end
  end
end

Now when you want to know what their referer is, it will either be a valid url or 'none'. Note that since it's in the session, it's not perfect: they could go to another url and come back and the session will still be valid.

like image 180
Jonathan Julian Avatar answered Oct 11 '22 08:10

Jonathan Julian


def save_referer
  session['referer'] = request.env["HTTP_REFERER"] || 'none' unless session['referer'] && !is_logged_in?
end

beautiful ;-)

like image 39
tim kretschmer Avatar answered Oct 11 '22 08:10

tim kretschmer