Yii Framework Forum: IP Address of current computer - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

IP Address of current computer Rate Topic: ****- 1 Votes

#1 User is offline   Coda 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 101
  • Joined: 20-September 10
  • Location:Kathmandu, Nepal

Posted 21 December 2010 - 01:58 AM

How can I get the IP of current user?
0

#2 User is offline   Maurizio Domba 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,229
  • Joined: 12-October 09
  • Location:Croatia

Posted 21 December 2010 - 02:24 AM

You have to decide if you need the IP of the current computer (in the topic name) or of the current user...

If you need the IP address of the user that is visiting your pages then you can use getUserHostAddress() - http://www.yiiframew...tAddress-detail
Find more about me.... btw. Do you know your WAN IP?
0

#3 User is offline   Coda 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 101
  • Joined: 20-September 10
  • Location:Kathmandu, Nepal

Posted 21 December 2010 - 03:08 AM

View Postmdomba, on 21 December 2010 - 02:24 AM, said:

You have to decide if you need the IP of the current computer (in the topic name) or of the current user...

If you need the IP address of the user that is visiting your pages then you can use getUserHostAddress() - http://www.yiiframew...tAddress-detail


I want to IP of computer from where the user has logged in or created new account to login. I want to use in login Model.
0

#4 User is offline   Maurizio Domba 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,229
  • Joined: 12-October 09
  • Location:Croatia

Posted 21 December 2010 - 03:32 AM

OK... getUserHostAddress() is what you need...

Example:
<?php echo Yii::app()->request->userHostAddress; ?>

Find more about me.... btw. Do you know your WAN IP?
1

#5 User is offline   Coda 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 101
  • Joined: 20-September 10
  • Location:Kathmandu, Nepal

Posted 21 December 2010 - 11:04 PM

View Postmdomba, on 21 December 2010 - 03:32 AM, said:

OK... getUserHostAddress() is what you need...
Example:
<?php echo Yii::app()->request->userHostAddress; ?>



Thank you mdomba.
0

#6 User is offline   Gustavo 

  • Master Member
  • Yii
  • Group: Moderators
  • Posts: 915
  • Joined: 27-July 10
  • Location:Curitiba - Brasil

Posted 22 December 2010 - 01:45 PM

userHostAddress does not return the actual ip always, depending on the user's internet provider
something like this would solve it

--
Extensions:
translate modue - module to handle translations
multiActiveRecord - db selection in models
redisCache - redis cache component
mpCpanel - interact with cpanel api
mUploadify - use uploadify uploader in your application

Gustavo Salomé Silva
0

#7 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,993
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 23 December 2010 - 06:00 AM

@Gustavo:
Can you explain, how a user should be able to fake the sender IP address? The $_SERVER['REMOTE_ADDR'] (used in getUserHostAddress()) is not related to the 'Host' header in the HTTP request. It's rather the IP address that PHP/Apache obtains from the network stack. I see no way how this numeric value could in any way get altered by the user.

EDIT:
Fixed typo, it's REMOTE_ADDR, not REMOTE_HOST

This post has been edited by Mike: 23 December 2010 - 06:02 AM

0

#8 User is offline   Maurizio Domba 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,229
  • Joined: 12-October 09
  • Location:Croatia

Posted 23 December 2010 - 06:41 AM

The user cannot alter it... but if someone uses a PROXY... then you can get the proxy ip or to be clear wan ip...

Some proxy's (not so effective ones) in turn set another variable to the original users wan ip... but if a proxy is good then you cannot get in any way the original user wan ip...

On top of that there are many variants that could be set by a proxy server like:

	$_SERVER["HTTP_VIA"]
	$_SERVER["CLIENT_IP"]
	$_SERVER["HTTP_CLIENT_IP"]
	$_SERVER["HTTP_FROM"]
	$_SERVER["HTTP_FORWARDED"]
	$_SERVER["HTTP_X_FORWARDED"]
	$_SERVER["HTTP_X_FORWARDED_FOR"]
	$_SERVER["HTTP_PROXY_REMOTE_ADDR"]
	$_SERVER["HTTP_PROXY_CONNECTION"]
	$_SERVER["HTTP_REMOTE_IP"]
	$_SERVER["HTTP_SCANNER_HOST"]

Find more about me.... btw. Do you know your WAN IP?
1

#9 User is offline   Gustavo 

  • Master Member
  • Yii
  • Group: Moderators
  • Posts: 915
  • Joined: 27-July 10
  • Location:Curitiba - Brasil

Posted 23 December 2010 - 08:22 AM

perfect answer from mdomba, nothing to add
Ps.: in the code I wrote in the link posted only checks for the most commom proxys, like aol and a couple others
--
Extensions:
translate modue - module to handle translations
multiActiveRecord - db selection in models
redisCache - redis cache component
mpCpanel - interact with cpanel api
mUploadify - use uploadify uploader in your application

Gustavo Salomé Silva
0

#10 User is offline   ignition25 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 24
  • Joined: 25-November 10

Posted 05 April 2011 - 12:58 AM

I'm using Yii::app()->request->userHostAddress but keep getting localhost (127.0.0.1) for every single user.
Any ideas why?
0

#11 User is offline   Maurizio Domba 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,229
  • Joined: 12-October 09
  • Location:Croatia

Posted 05 April 2011 - 01:17 AM

The IP address belongs to the computer you are connecting from... not to a user... so if you have the web server on the same computer you are working your IP address is "localhost" = "127.0.0.1" ...
Find more about me.... btw. Do you know your WAN IP?
0

#12 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,993
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 05 April 2011 - 01:22 AM

ignition25:
If you talk about a public server, do you maybe have a reverse proxy in front of your Apache (like nginx)?
0

#13 User is offline   ignition25 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 24
  • Joined: 25-November 10

Posted 19 April 2011 - 09:34 AM

View PostMike, on 05 April 2011 - 01:22 AM, said:

ignition25:
If you talk about a public server, do you maybe have a reverse proxy in front of your Apache (like nginx)?


Yep, I have nginx sitting in front of Apache
0

#14 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,993
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 19 April 2011 - 09:50 AM

nginx will then create a new request to Apache. By default these requests will originate from localhost (because that's where nginx is). You can try this setting in nginx config:

proxy_set_header        REMOTE_ADDR $remote_addr;

0

#15 User is offline   ignition25 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 24
  • Joined: 25-November 10

Posted 01 May 2011 - 07:38 PM

View PostMike, on 19 April 2011 - 09:50 AM, said:

nginx will then create a new request to Apache. By default these requests will originate from localhost (because that's where nginx is). You can try this setting in nginx config:

proxy_set_header        REMOTE_ADDR $remote_addr;



Ah yes, of course. Thanks for the heads up.

Decided to use the Apache Request Header: "X-Forwarded-For" to grab this info.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users