Yii Framework Forum: Get Real Ip Address - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Get Real Ip Address Rate Topic: -----

#1 User is offline   Aneesh Asokan 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 237
  • Joined: 26-September 12

Posted 28 January 2013 - 02:33 AM

I'm ing this code to display the IP address.
<?php
function GetRealIPAddress()
{
    if (isset($_SERVER["HTTP_CLIENT_IP"]))
    {
        return $_SERVER["HTTP_CLIENT_IP"];
    }
    elseif (isset($_SERVER["HTTP_X_FORWARDED_FOR"]))
    {
        return $_SERVER["HTTP_X_FORWARDED_FOR"];
    }
    elseif (isset($_SERVER["HTTP_X_FORWARDED"]))
    {
        return $_SERVER["HTTP_X_FORWARDED"];
    }
    elseif (isset($_SERVER["HTTP_FORWARDED_FOR"]))
    {
        return $_SERVER["HTTP_FORWARDED_FOR"];
    }
    elseif (isset($_SERVER["HTTP_FORWARDED"]))
    {
        return $_SERVER["HTTP_FORWARDED"];
    }
    else
    {
        return $_SERVER["REMOTE_ADDR"];
    }
}
echo GetRealIPAddress();
?>


But it show like this.
::1


How can i get my real ip?
Thanks
Aneesh.
0

#2 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,481
  • Joined: 04-March 10
  • Location:UK

Posted 28 January 2013 - 03:41 AM

I believe that what you're seeing is the IPV6 version of localhost. I suspect that it will work once you're logging in from a different machine.

Just checked and confirmed here.
1

#3 User is offline   Aneesh Asokan 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 237
  • Joined: 26-September 12

Posted 28 January 2013 - 04:44 AM

View PostKeith, on 28 January 2013 - 03:41 AM, said:

I believe that what you're seeing is the IPV6 version of localhost. I suspect that it will work once you're logging in from a different machine.

Just checked and confirmed here.


How we can access the public IP address?
Thanks
Aneesh.
1

#4 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,481
  • Joined: 04-March 10
  • Location:UK

Posted 28 January 2013 - 05:10 AM

You shouldn't need to, the public IP will appear when you're not connecting from the dev machine.

If you need to see what's available, try seeing what's held in the $_SERVER array:

print_r($_SERVER);

0

#5 User is offline   janisto 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 45
  • Joined: 30-March 11
  • Location:Helsinki, Finland

Posted 28 January 2013 - 06:00 AM

I'm using this in my config:

/**
 * Get client IP address
 * @return string
 */
function getIpAddress() {
	foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key) {
		if (array_key_exists($key, $_SERVER) === true) {
			foreach (explode(',', $_SERVER[$key]) as $ip) {
				$ip = trim($ip);
				if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false) {
					return $ip;
				}
			}
		}
	}
}


// client IP address
$ip = getIpAddress();
$ip = (is_null($ip)) ? '' : $ip;

0

#6 User is offline   outrage 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 313
  • Joined: 10-November 09
  • Location:Blackpool, United Kingdom

Posted 28 January 2013 - 03:52 PM

You don't seem to understand so I'll re-iterate a previous comment.

::1 is being returned because you are connecting to your local machine (localhost).

When you deploy the application to a remote web server, your real IP will be returned.

If you wish to do some testing (geoip or something similar), then check if the IP = ::1 and set it to your real ip.

if ($ip == '::1') {
 $ip = '123.456.789.123'; // enter your real IP address here
}


Your real IP can easily be found by Googling "what is my ip address".
0

#7 User is offline   Aneesh Asokan 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 237
  • Joined: 26-September 12

Posted 29 January 2013 - 01:10 PM

Thanks to all replys
Thanks
Aneesh.
0

#8 User is offline   p0pemar1naru 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 30-July 12
  • Location:Toronto, ON

Posted 06 February 2013 - 02:05 PM

Here is my non intrusive implementation.

0) You will test it like this. In any Controller action, add:

echo '<pre>'; print_r(Yii::app()->request->getUserHostAddress()); exit(0);


1) in protected/config/main.php, under the components section add:

        'request'=>array( 
            'class' => 'NTDI_CHttpRequest',
        ),


this will tell Yii Fw to use the new NTDI_CHttpRequest class instead the core CHttpRequest class when dealing with Http Request.

2) in protected/components/ make a new file with the same name as the new class name .php

NTDI_CHttpRequest.php file:

<?php

interface HttpRequestable {
    public function getUserHostAddress();
}

/**
 * NTDI_CHttpRequest offers an improved version of the CHttpRequest::getUserHostAddress()
 * The method, is a port from Codeigniter w some small mods.
 * 
 * If your server is behind a reverse proxy, you must whitelist the proxy IP
 * addresses the Framework should trust the HTTP_X_FORWARDED_FOR
 * header in order to properly identify the visitor's IP address.
 * 
 * @author p0pemar1naru
 */
class NTDI_CHttpRequest extends CHttpRequest implements HttpRequestable {

    /**
     * Reverse Proxy IPs.
     * @var string Comma-delimited, e.g. '10.0.1.200,10.0.1.201'
     */
    protected $ntdi_proxyIP = '';

    /**
     * IP address of the current user
     * @var boolean
     */
    private $ntdi_IPAddress = FALSE;

    public function init()
    {
        parent::init();
    }

    /**
     * Fetch the IP Address. Determines and validates the visitor's IP address.
     * @return string IP address
     */
    public function getUserHostAddress()
    {
        if ($this->ntdi_IPAddress !== FALSE)
            return $this->ntdi_IPAddress;
        if ($this->ntdi_proxyIP != '' && isset($_SERVER['HTTP_X_FORWARDED_FOR']) && isset($_SERVER['REMOTE_ADDR'])) {
            $proxies = preg_split('/[\s,]/', $this->ntdi_proxyIP, -1, PREG_SPLIT_NO_EMPTY);
            $proxies = is_array($proxies) ? $proxies : array($proxies);
            $this->ntdi_IPAddress = in_array($_SERVER['REMOTE_ADDR'], $proxies) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
        } elseif (isset($_SERVER['REMOTE_ADDR']) && isset($_SERVER['HTTP_CLIENT_IP']))
            $this->ntdi_IPAddress = $_SERVER['HTTP_CLIENT_IP'];
        elseif (isset($_SERVER['REMOTE_ADDR']))
            $this->ntdi_IPAddress = $_SERVER['REMOTE_ADDR'];
        elseif (isset($_SERVER['HTTP_CLIENT_IP']))
            $this->ntdi_IPAddress = $_SERVER['HTTP_CLIENT_IP'];
        elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
            $this->ntdi_IPAddress = $_SERVER['HTTP_X_FORWARDED_FOR'];

        if ($this->ntdi_IPAddress === FALSE) {
            $this->ntdi_IPAddress = '0.0.0.0';
            return $this->ntdi_IPAddress;
        }
        if (strpos($this->ntdi_IPAddress, ',') !== FALSE) {
            $x = explode(',', $this->ntdi_IPAddress);
            $this->ntdi_IPAddress = trim(end($x));
        }
        if (!$this->ntdi_isValidIP($this->ntdi_IPAddress))
            $this->ntdi_IPAddress = '0.0.0.0';
        return $this->ntdi_IPAddress;
    }

    /**
     * Validate IP Address
     * @param string $ip
     * @return boolean
     */
    public function ntdi_isValidIP($ip)
    {
        $ip_segments = explode('.', $ip);
        // Always 4 segments needed
        if (count($ip_segments) != 4)
            return FALSE;
        // IP can not start with 0
        if ($ip_segments[0][0] == '0')
            return FALSE;
        // Check each segment
        foreach ($ip_segments as $segment) {
            // IP segments must be digits and can not be
            // longer than 3 digits or greater then 255
            if ($segment == '' || preg_match("/[^0-9]/", $segment) || $segment > 255 || strlen($segment) > 3)
                return FALSE;
        }
        return TRUE;
    }

}



NOTE: the role of the interface is to give me a clear understanding of what method was overriden from core class.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

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