在输入表单时跟踪客户端IP地址并存储在SQL表中

时间:2014-10-18 18:24:51

标签: php mysql forms

我有PHP表单处理数据到sql表我有两个输入字段

  • 我需要一个隐藏字段,隐藏字段应该跟踪客户端IP地址,当提交表单时,我需要客户端地址也保存在表中

PHP表单处理

<?php
$db_username = "sanoj";
$db_password = "123456";
try {
#connection 
$conn = new PDO('mysql:host=localhost;dbname=localtest', $db_username, $db_password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$data = $conn->prepare('INSERT INTO test (first, last) VALUES (:first, :last)');
$first = filter_input(INPUT_POST, 'first', FILTER_SANITIZE_STRING);
$last = filter_input(INPUT_POST, 'last', FILTER_SANITIZE_STRING);
$data->execute(array(':first' => $first, ':last' => $last,));
#exception handiling
} catch (PDOException $e) {
echo $e->getMessage();
}
?>

有些人可以帮助我,我找到了例子,很难理解What is the most accurate way to retrieve a user's correct IP address in PHP?中给出的这些代码,因为刚开始提前感谢

2 个答案:

答案 0 :(得分:1)

我会用它来检索某人的IP

$_SERVER["REMOTE_ADDR"]

答案 1 :(得分:0)

$REMOTE_ADDR = $_SERVER['REMOTE_ADDR'];/*add this line*/
$data->execute(array(':first' => $first, ':last' => $last, ':REMOTE_ADDR' => $REMOTE_ADDR));/* edit this line*/