How do I get the the full URL of an incoming request


#1

Hey,

Is is possible to get the full URL
(‘http://www.sender-domain.com/links/list.html’)

from where a user was sent to my site
(www.my-domain.com/controller/action) e.g. by clicking on a link? (I
read about something called a ‘request’ object, I don’t know what it is
or does, is that what I’m looking for?

I’m very new to Rails so I’d appreciate any help!!

Thanks,
Gustav

e-mail: removed_email_address@domain.invalid

“Touched by His noodly appendage”,
www.venganza.org


#2

gustav paul wrote:

Hey,

Is is possible to get the full URL
(‘http://www.sender-domain.com/links/list.html’)

from where a user was sent to my site
(www.my-domain.com/controller/action) e.g. by clicking on a link? (I
read about something called a ‘request’ object, I don’t know what it is
or does, is that what I’m looking for?
It should be available in the controller as
@request.env[“HTTP_REFERER”].


#3

Confirmed!

In a simple controller action, I put:
@referringPageURL = @request.env[“HTTP_REFERER”]

In the corresponding view I entered:
referring page = <%= @referringPageURL %>

To confirm, I added a standard HTML link to the page in the
/public/index.html file, and I also cloned that file as
/public/index2.html and also followed the link from there.

Note that request.env[“HTTP_REFERER”] also seems to work.

http://api.rubyonrails.org/classes/ActionController/Base.html hints that
to get the IP address where the request came from, you can use
request.env[“REMOTE_IP”] in the controller.

Hope this helps!

Thanks,
Dominique

Alex Y. wrote:

gustav paul wrote:

Hey,

Is is possible to get the full URL
(‘http://www.sender-domain.com/links/list.html’)

from where a user was sent to my site
(www.my-domain.com/controller/action) e.g. by clicking on a link? (I
read about something called a ‘request’ object, I don’t know what it is
or does, is that what I’m looking for?
It should be available in the controller as
@request.env[“HTTP_REFERER”].


#4

request.remote_ip() works better as it handles proxied IPs also.

Again, HTTP_REFERER is not dependable. Some clients send it some don’t.

Bob S.
http://www.railtie.net/


#5

Unfortunately, HTTP_REFERER is useless nowadays. There is no stable
method
of getting that information.

Bob S.
http://www.railtie.net/


#6

so whats the best way to handle a login controller that uses a before
filter to catches users who arent logged in.

How do you enable the user to go to the page they were trying to get to?

Ive been having problems with request.env[“HTTP_REFERER”] as well

When I have a complex url like /books?id=5&c=4

then it works, but if it is simple like

/books, it doesnt work correctly. Anyone figure this out?


#7

Eric G. wrote:

/books, it doesnt work correctly. Anyone figure this out?

I do this by having a before_filter on the protected page that stores
the path of that page in a session variable, using request.path:

session[:requested_page] = request.path
flash[:notice] = “Please log in first.”
redirect_to(:controller => ‘login’)

Then when they log in successfully, you can just do

redirect_to(session[:requested_page])

It’s handled every URL I’ve thrown at it so far! It also feels a bit
more reliable than trusting the HTTP_REFERER environment variable.

Chris


#8

I tried all the methods listed in this thread, none of them works
correctly with IE 6. For example if my url is /books/view?book=1, the
best that any method returns is

“books/view”

In other words, the parameters are left out.

Jason N. wrote:

Have you tried using request.parameters? Way sweeter as it gives you the
same hash that was originally generated form your routes.


#9

Have you tried using request.parameters? Way sweeter as it gives you the
same hash that was originally generated form your routes.


#10

i have the following url im trying to
save:

http://127.0.0.1:3000/photos/view?book=1&id=2

The problem is, when I use url_for {request.parameters},

it doesnt include the ?book=1&id=2 part. However, when I do “puts
request.parameters[:id]”, I get “2”

Whats going on here? Thanks in advance…


#11

i cant look at my code at home right now as i’m in the office, but i
think i used this :

redirect_to request.request_uri