The php function getUserIpAddr() yields an array, [$ip] when called from my index.php. I need to know how to extract just the ip from it so that I can use php function inet_pton before inserting the ip in the database.
Code for getUserIpAddr():
“`
<?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;
}
?>
Code in index.php:
“`
<?php
session_start();
$ip = null;
$php_scripts = ‘../php/’;
require $php_scripts . ‘PDO_Connection_Select.php’;
require $php_scripts . ‘GetUserIpAddr.php’;
$ip = GetUserIpAddr();
if (!$pdo = PDOConnect(“test”)):
{
echo “Failed to connect to database” ;
exit;
}
else:
{
/* need code to extract ip from [$ip] */
$ip = inet_pton([$ip]);
$stmt = $pdo->prepare(“INSERT INTO download (ADDRESS) VALUES (?)”);
$stmt->execute($ip) ;
}
endif;
?>
Thanks in advance