Codeigniter get current ip address We can use input class to get the customer ip address. Input class provides inbuilt functions to get and validate ip address. Here in this tutorial we are going to explain how you can get ip address using input class in Codeigniter.
Codeigniter get current ip address Example
You can get ip address using the following syntax-
$this->input->ip_address();
The above syntax will return the current user’s ip and if ip addres is invalid it returns 0.0.0.0.
The input class is automatically loaded so need not to load manually, Just use – $this->input->ip_address(); to get the ip address.
Validate Ip Address
You can validate ip address using the below syntax-
$ip = "Your_IP"; $this->input->valid_ip($ip);
Pass the IP address to validate in valid_ip() function. It will return true if IP is valid and return false if IP is invalid.
Example
Here is an example of validate ip address.
$ip ='109.77.98.92'; $isValidIp = $this->input->valid_ip($ip); if($isValidIp){ echo "Ip is valid"; }else{ echo "Ip is Not Valid"; }