Explain with code how to get a user's IP address.
The users IP address can be returned by specifying the host name as aparameter in gethostbyname(“hostname”) method.
Example:<?php
$ip = gethostbyname('www.sample.com');
echo $ip;
?>
Explain with code how to get a user's IP address.
Identifying the IP address is a very important requirement, using which the visitor’s details could be persisted. For certain security considerations, IP address can be stored, if at all they purchase or reorder items, or to know about the geographical location of the visitor.
The following code snippet is used for finding IP address-
$ipAddress=@$REMOTE_ADDR;
echo "<b>IP Address= $ipAddress</b>";
If the register_global is off in php.ini file, then the following script is to be used.
$ipAddress=$_SERVER['REMOTE_ADDR'];