SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru DeNasio's Avatar
    Join Date
    May 2001
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Best way to get IP address?

    What is the best method to get the users IP address? I've seen a couple of codes on the Internet to get the users IP address, like these:

    PHP Code:
    $ipaddress $_SERVER["REMOTE_ADDR"]; 
    PHP Code:
    $ipaddress getenv('REMOTE_ADDR'); 
    PHP Code:
    $ipaddress = @$REMOTE_ADDR
    and even this

    PHP Code:
    function getRealIpAddr()
    {
        if (!empty(
    $_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
        
    {
          
    $ip=$_SERVER['HTTP_CLIENT_IP'];
        }
        elseif (!empty(
    $_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
        
    {
          
    $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
        }
        else
        {
          
    $ip=$_SERVER['REMOTE_ADDR'];
        }
        return 
    $ip;

    Ballot-Box.net - free polls for webmasters
    FormLog.com - free form processor

  2. #2
    SitePoint Member
    Join Date
    Sep 2012
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DeNasio View Post
    PHP Code:
    function getRealIpAddr()
    {
        if (!empty(
    $_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
        
    {
          
    $ip=$_SERVER['HTTP_CLIENT_IP'];
        }
        elseif (!empty(
    $_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
        
    {
          
    $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
        }
        else
        {
          
    $ip=$_SERVER['REMOTE_ADDR'];
        }
        return 
    $ip;

    That is the one I use, but to my knowledge there is no way to guarantee the IP you get. I wouldn't rely on it.

  3. #3
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    You should never ever use anything other then "$_SERVER["REMOTE_ADDR"]" alright. That function "getRealIpAddr" DO NOT USE THAT! The only time you shoulde use "HTTP_CLIENT_IP" or "HTTP_X_FORWARDED_FOR" is when you are behind a known proxy or load balancer. Under any other condition you must use "$_SERVER["REMOTE_ADDR"]" only.


    Repeat: If your server is not behind a proxy or load balancer, only ever use "$_SERVER["REMOTE_ADDR"]"

    HTTP_CLIENTT_IP, HTTP_X_FORWARDED_FOR, are both user defined and can contain garbage data.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •