How to Get IP Address and get Information of User in PHP
PHP Exercises, Practice and Solution: Write a PHP script to get the client IP address.
Getting The Client Information (Client's IP Address,Operating System,Browser Name,Device Type) in PHP
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 in PHP.
- $_SERVER['REMOTE_ADDR'] – Returns the IP address of the user from which viewing the current page.
Methods
get_ip()
get_os()
get_browser()
get_device()
get_ip()
If you want to get the client IP Address, Use this Method, This Method will return Client IP Address
Example
echo UserInfo::get_ip()
get_OS()
If you want to get the client Operating System Name, Use this Method, This Method will return Client Operating System
Example
echo UserInfo::get_os();
get_Browser()
If you want to get the client's Browser Name, Use this Method, This Method will return Client's Browser Name
Example
echo UserInfo::get_browser();
get_Device()
If you want to get the client's Device Type Then Use this Method, This Method will return Client's Device Type Name Such as Mobile,Tablet,Computer
Example
echo UserInfo::get_device();
IP address: An Internet Protocol address (IP address) is a numerical label assigned to each device connected to a computer network that uses the Internet Protocol for communication. An IP address serves two principal functions: host or network interface identification and location addressing.
Internet Protocol version 4 (IPv4) defines an IP address as a 32-bit number. However, because of the growth of the Internet and the depletion of available IPv4 addresses, a new version of IP (IPv6), using 128 bits for the IP address, was developed in 1995, and standardized in December 1998. In July 2017, a final definition of the protocol was published. IPv6 deployment has been ongoing since the mid-2000s.
IP addresses are usually written and displayed in human-readable notations, such as 172.16.254.2 in IPv4, and 2001:db8:0:1234:0:567:8:1 in IPv6. The size of the routing prefix of the address is designated in CIDR notation by suffixing the address with the number of significant bits, e.g., 192.168.1.16/24, which is equivalent to the historically used subnet mask 255.255.255.0.
The IP address space is managed globally by the Internet Assigned Numbers Authority (IANA), and by five regional Internet registries. They are responsible in their designated territories for assignment to end users and local Internet registries, such as Internet service providers. IPv4 addresses have been distributed by IANA to the RIRs in blocks of approximately 16.8 million addresses each. Each ISP or private network administrator assigns an IP address to each device connected to its network.
Sample Solution: -
PHP Code:
class UserInfo{ private static function get_user_agent() { public static function get_ip() { public static function get_os() { $user_agent = self::get_user_agent(); foreach ($os_array as $regex => $value) { public static function get_browser() { $user_agent= self::get_user_agent(); $browser = "Unknown Browser"; $browser_array = array( foreach ($browser_array as $regex => $value) { if (preg_match($regex, $user_agent)) { } return $browser; } public static function get_device(){ $tablet_browser = 0; if (preg_match('/(tablet|ipad|playbook)|(android(?!.*(mobi|opera mini)))/i', strtolower($_SERVER['HTTP_USER_AGENT']))) { if (preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|android|iemobile)/i', strtolower($_SERVER['HTTP_USER_AGENT']))) { if ((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml') > 0) or ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))) { $mobile_ua = strtolower(substr(self::get_user_agent(), 0, 4)); if (in_array($mobile_ua,$mobile_agents)) { if (strpos(strtolower(self::get_user_agent()),'opera mini') > 0) { if ($tablet_browser > 0) { } //echo UserInfo::get_ip(); echo UserInfo::get_device(); echo UserInfo::get_os(); echo UserInfo::get_browser(); ?> |
Flowchart: