Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

most reliable way to get ip address of users using php? [duplicate]

Possible Duplicate:
Getting Users Real IP address using PHP

I want to log the ip address of anyone who visits my website using a php script to block possible spammers. Is there a surefire way to find and log anyone's real ip address or a most reliable method?

UPDATE: my website only asks users for a password to log in, no username required. If I want to keep my current method of validation, what other methods can I use to block or deter most spammers?

like image 814
user701510 Avatar asked Dec 05 '22 22:12

user701510


2 Answers

Function to find real IP address in PHP

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;
}

Source:- http://roshanbh.com.np/2007/12/getting-real-ip-address-in-php.html

like image 197
Mukesh Chapagain Avatar answered Dec 10 '22 11:12

Mukesh Chapagain


As Dagon said, $_SERVER['REMOTE_ADDR'], at least using Apache.

But then again, it's already in the Apache logs.

like image 30
jcomeau_ictx Avatar answered Dec 10 '22 11:12

jcomeau_ictx