Many times we need to collect the visitor IP address to track activity and for security reasons. It’s very easy to get the IP address of visitors in PHP. PHP $_SERVER variable provides an easy way to get user IP address. The $_SERVER contains an array that provides the server and environment-related information in PHP.
The simplest way to get the visitor IP address is using the REMOTE_ADDR indices in PHP $_SERVER method.
$_SERVER['REMOTE_ADDR']
– Returns the IP address of the user from which viewing the current page.echo 'User IP - '.$_SERVER['REMOTE_ADDR'];
But sometimes REMOTE_ADDR does not return the correct IP address of the user. The reason behind this is to use a Proxy or CDN. In that situation, use the following code to get real IP address of user in PHP.
function getUserIpAddr(){
if(!empty($_SERVER['HTTP_CLIENT_IP'])){
//ip from share internet
$ip = $_SERVER['HTTP_CLIENT_IP'];
}elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
//ip pass from proxy
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}else{
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
echo 'User Real IP - '.getUserIpAddr();
Thank you so much, I just got it so easily
Realy Helpfull Thanxxxxxxxx
very helpfull
Help me for including voice recognition in php
Sir , Can you please help me how can I store it in my database.
Thank you
Thanks for the simple code.
THANKS A LOT.
Can you help me, How can I store it in some database and view at the current time when users are browsing the website? I will be thankful if you help.
Thank you so much for the guide
I just love you guys, you make everything so simple.