How to get the client address

1,948 views
Skip to first unread message

lichan...@gmail.com

unread,
Jan 19, 2015, 10:21:21 PM1/19/15
to shiny-...@googlegroups.com

Hi,
I would like to get the client ip address at server side, but actually

session$clientData$url_hostname always provides the server name not client name,
no matter which computer i used to login my server. And any other ways to get client or
client proxy IP ?

Thank you so much!

Joe Cheng

unread,
Jan 20, 2015, 1:38:42 AM1/20/15
to lichan...@gmail.com, shiny-...@googlegroups.com
Try session$request$REMOTE_ADDR. Note that if you're going through a proxy server, then this will be the address of the proxy server, not the client.

--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/7febaf26-82ba-4773-9d3c-9801c3ab5058%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

lichan...@gmail.com

unread,
Jan 20, 2015, 1:23:46 PM1/20/15
to shiny-...@googlegroups.com, lichan...@gmail.com
Thanks joe,
  But, It always give me 127.0.0.1, my shiny-server is on a remote computer and I accessed the App through my computer's browse, but it doesn't show the IP address of my computer on the page, always 127.0.0.1.(
output$clientip<-renderText({
  session$request$REMOTE_ADDR
})
)
where is the problem? Thank you  for your kind guidance!

Best,

CJ

Joe Cheng

unread,
Jan 20, 2015, 2:40:42 PM1/20/15
to lichangwchina, shiny-...@googlegroups.com
Maybe try session$request$HTTP_X_FORWARDED_FOR. However, note that this header can be forged:

The usefulness of XFF depends on the proxy server truthfully reporting the original host's IP address; for this reason, effective use of XFF requires knowledge of which proxies are trustworthy, for instance by looking them up in a whitelist of servers whose maintainers can be trusted.

Lichang Wang

unread,
Jan 20, 2015, 3:09:56 PM1/20/15
to Joe Cheng, shiny-...@googlegroups.com
Thanks Joe,
session$request$HTTP_X_FORWARDED_FOR returned nothing, I also tried HTTP_CLIENT_IP, but just session$request$REMOTE_ADDR can give me 127.0.0.1.  when I printed  session$request$HTTP_X_FORWARDED_FOR  it shows me 'null'.   I'm using https:// (ssl) not http. If I need to enable session$request$HTTP_X_FORWARDED_FOR in somewhere? now, I use ajax to get the client ip from client page.

Thank you!

Best,

Lichang

Joe Cheng

unread,
Jan 20, 2015, 5:10:40 PM1/20/15
to Lichang Wang, shiny-...@googlegroups.com
Can you describe your server setup? Do you know what proxies are sitting between your client and the R process? Are you using Shiny Server Pro, and if not, how are you getting https support?

Vincent

unread,
Jan 22, 2015, 4:59:44 AM1/22/15
to shiny-...@googlegroups.com, lichan...@gmail.com
I have the same issue with both shiny server and shiny server pro. I have tried everything I can think of (and can find online) to access the elements of session and the best I can do is 127.0.0.1. I tried all the options that Lichan mentioned as well.

access_log in shiny_server.cfg works fine by the way so it is possible to get the ip information apparently. I just cannot access it from within R/Shiny

Is there anything in session (or elsewhere) that would allow R/Shiny to recognize a user after a browser refresh? IP seems the obvious choice but ...

Vincent

unread,
Jan 22, 2015, 5:23:11 AM1/22/15
to shiny-...@googlegroups.com, lichan...@gmail.com
FYI I am using http, not https

Joe Cheng

unread,
Jan 22, 2015, 2:09:17 PM1/22/15
to Vincent, shiny-...@googlegroups.com, lichangwchina
Oh, please don't use IP address for this. If two different users come from behind the same router (like they work at the same company or are in the same house) then you'll confuse them for the same person.

I suppose asking them to log in is not acceptable?

I guess another option would be cookies--we're not set up to let you set those right now but it's conceivable I suppose. What does your scenario look like?

Vincent

unread,
Jan 22, 2015, 8:20:44 PM1/22/15
to shiny-...@googlegroups.com, vincen...@gmail.com, lichan...@gmail.com
Thanks for the comment Joe. My reply is give below. In an attempt to avoid a complete thread-jacking, could you suggest a way to get the ip address from inside a shiny app? None of the things Lichang or I have tried have been successful so far. Is the process used to put the ip address into the access_log not accessible inside Shiny/R?


I agree with you about the limitations of an ip address as an identifier. Students, at least when they are campus, will have a unique ip. Still not ideal, I agree. It would be great if there was information in the session object that could identify a browser session and that would survive a browser refresh. This is important for me because a student might spend a lot of time running analyses in the app and would be very unhappy to lose it all due to an accidental tab close or refresh.
 
The way this works in my app now is that the user can load and save the state and won't lose state on browser refresh. To load and activate a previous state I use a browser refresh (I have a LOT of inputs so using something like updateXXX for all of them might be a hassle). Basically the state is dumped into the global environment and then pulled in when the browser is refreshed. Once that is done the global variables (data and inputs) are deleted. Perhaps cookies could work if they provide a reference label that could be used as input to something like get() in R. 

It would be great if there was something in session$request that you could use to assign a user id. Sort of like http://shiny.rstudio.com/gallery/chat-room.html but then with (some) memory for the user, i.e., you keep the id after browser refresh.

Creating a login might be an option. Question: How would you access the user id? Would it be through session$request? If so, why would that work when other attempts have not? I looked at the documentation but there is not much mentioned about this as far as I can tell.

Thanks Joe

Joe Cheng

unread,
Jan 28, 2015, 12:24:47 PM1/28/15
to Vincent, shiny-...@googlegroups.com, lichangwchina
I thought about this some more, I think the right answer is to use history.replaceState in the browser to "tag" the browser with a session ID. I'll see if I can come up with an example for you.

Lichang Wang

unread,
Jan 28, 2015, 12:47:55 PM1/28/15
to shiny-...@googlegroups.com
Hi,
Sorry for the delay to reply, I get the IP from javascript eventually.  It will dependent on some third party service http://stackoverflow.com/questions/391979/get-client-ip-using-just-javascript, then you can use "Shiny.onInputChange()" send it to service. Of course, you can setup the third party service on you server using PHP(on NGINX server).

Best,

Lichang

On Mon, Jan 19, 2015 at 7:21 PM, <lichan...@gmail.com> wrote:

--
You received this message because you are subscribed to a topic in the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/shiny-discuss/9WcbS3E4Cfc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to shiny-discus...@googlegroups.com.

Joe Cheng

unread,
Jan 28, 2015, 2:01:21 PM1/28/15
to Lichang Wang, shiny-...@googlegroups.com
Here's a proof of concept:

It's just a Shiny app, not a package, so you can try it with:
shiny::runGitHub("jcheng5/shiny-resume")

Run the app, then click the button and change the numeric input, then hit Reload (or copy and paste the URL to a different browser).

--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/CAFh9p0DiWSAA8whaawsUDj%2BmpvD%2BO3T6Pw0avMxWrNHQMXaHjw%40mail.gmail.com.

Vincent

unread,
Jan 28, 2015, 5:06:30 PM1/28/15
to shiny-...@googlegroups.com, lichan...@gmail.com
Thanks Joe!

Karim Mezhoud

unread,
Sep 18, 2016, 4:02:19 PM9/18/16
to Shiny - Web Framework for R
Hi,

system('ipconfig getifaddr en0')

192.168.1.70

But I can't capture IP with

Capture.output(system('ipconfig getifaddr en0'))


jeremiah rounds

unread,
Sep 18, 2016, 4:33:18 PM9/18/16
to Karim Mezhoud, Shiny - Web Framework for R
system('ipconfig getifaddr en0', intern=TRUE)

--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discuss+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/08c5e520-a585-48d6-8f95-7b0958c0f7b4%40googlegroups.com.

Karim Mezhoud

unread,
Sep 18, 2016, 4:46:26 PM9/18/16
to Shiny - Web Framework for R, kmez...@gmail.com
Thanks!


On Sunday, September 18, 2016 at 9:33:18 PM UTC+1, jeremiah rounds wrote:
system('ipconfig getifaddr en0', intern=TRUE)
On Sun, Sep 18, 2016 at 1:02 PM, Karim Mezhoud <kmez...@gmail.com> wrote:
Hi,

system('ipconfig getifaddr en0')

192.168.1.70

But I can't capture IP with

Capture.output(system('ipconfig getifaddr en0'))


--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.

Fernando Roa

unread,
Nov 17, 2016, 11:54:06 AM11/17/16
to Shiny - Web Framework for R
This works, I tried it in shinyapps.io using mongodb to get the ip info.
Reply all
Reply to author
Forward
0 new messages