I've got a LAMP stack where i've set up the following code in a php file:
if (isset($_POST['name'], $_POST['age'])) {
$db = new Mysqli("localhost", "test", "123", "postdata");
$name = $db->real_escape_string($_POST['name']);
$age = (int)$_POST['age'];
$query = "INSERT INTO data SET mydata='$name, $age, $ip'";
$db->query($query);
}
when i curl data via the cmd, e.g. "curl -d name=peter -d age=32 localhost/isproject/test.php", i'd also want to save the IP address and relay it to the MySQL DB to make sure that the entry with the name and age also comes with the transmitting IP.
I've checked for multiple soultions and used the following code:
if(!empty($_SERVER["HTTP_CLIENT_IP"])){
$ip = $_SERVER["HTTP_CLIENT_IP"];
}else if(!empty($_SERVER["HTTP_X_FORWARDED_FOR"])){
$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
}else{
$ip = $_SERVER["REMOTE_ADDR"];
}
However, this results in the entry of the following image:
Is this due to incorrect use of code or simply due to the fact that i curled from the device hosting the server? Regardless i'm a newbee within this area and i'd love some feedback!