A simple function to find the real IP address of the client with php
As the internet growing as long as spamming,hacking,fishing,bots also increasing,there by normal user can easly decepted by with them. in many security application we need to find the real ip address to ban or record the ip related activities.here we use small function that returns and modify the PHP global remote address variable.
Usually we’ll use $_SERVER['REMOTE_ADDR'] to get clients IP address. But this doesn’t return the real IP address all time. Basically when someone using PROXY that valiable returns proxy IP the client using. So here’s a function you can use to detect real ip address of client. Here are extra Server variable which might be available to determine the exact IP address of the client’s machine in PHP, they are HTTP_CLIENT_IP and HTTP_X_FORWARDED_FOR
1 2 3 4 5 6 7 8 | function real_ip(){ if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){ $_SERVER['REMOTE_ADDR']=$_SERVER['HTTP_X_FORWARDED_FOR']; }elseif(isset($_SERVER['HTTP_X_REAL_IP']){ $_SERVER['REMOTE_ADDR']=$_SERVER['HTTP_X_REAL_IP']; } return $_SERVER['REMOTE_ADDR']; } |
