How to get IP Address of user in PHP or How to avoid spam users from website?



Hi All,

Many website owner should notice spam or people with unnecessary data, You might feel bad and want
to stop them but you don't know how.

There could be many ways  but I started blocking them using panel. but you need correct IP Address to block that person only.

In PHP , Please use below code to get IP Address and save this ip address in database table where you are storing email id etc or as you need this.


// Function to get the client IP address
function get_client_ip() {
    $ipaddress = '';
    if (isset($_SERVER['HTTP_CLIENT_IP']))
        $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
    else if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
        $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
    else if(isset($_SERVER['HTTP_X_FORWARDED']))
        $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
    else if(isset($_SERVER['HTTP_FORWARDED_FOR']))
        $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
    else if(isset($_SERVER['HTTP_FORWARDED']))
        $ipaddress = $_SERVER['HTTP_FORWARDED'];
    else if(isset($_SERVER['REMOTE_ADDR']))
        $ipaddress = $_SERVER['REMOTE_ADDR'];
    else
        $ipaddress = '';
    return $ipaddress;
}

** Best way to get authorized login is Google authrization login , facebook login, linkedin login etc .

Nothing is perfect but it better to do something then adding nothing.
Please let me know you thoughts.

Thanks
Abhi

Comments