【发布时间】:2013-04-25 16:38:18
【问题描述】:
我有一个 MySQL 查询,它将表单的内容插入到我的数据库中。
除了插入这些值之外,我还想发现并插入用户 IP 地址。这是为了网站的保护。这样,如果用户不断发布有害内容,他们的 ip 就会被屏蔽。
这是我的脚本。我正在尝试使用" . $_SERVER['REMOTE_ADDR'] . ",但它对我不起作用。有人可以告诉我这样做的方法吗?
我正在尝试将 IP 存储在我的表中名为“ip”的列中(int(10) 属性:UNSIGNED NULL:No Default:None)
更新:
当我将 'ip' 列更改为 VARCHAR(48) 时,我会在列中打印此内容,而不是完整的 IP:::1
<?php
ob_start();
// check if the review form has been sent
if(isset($_POST['review_content'])) {
if(isset($_POST['review_recipient'])) {
$content = $_POST['review_content'];
$review_recipient = $_POST['review_recipient'];
//We remove slashes depending on the configuration
if(get_magic_quotes_gpc()) {
$content = stripslashes($content);
$review_recipient = stripslashes($review_recipient);
}
$regex = "/(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w\.-]*)*\/?/";
$replacement = "[blocked url]";
$regex2 = "/(.*)\b(BLOCKED WORDS GO HERE!!!!)\b(.*)/";
$replacement2 = "[blocked content]<br/><br/>This content was blocked because it was deemed offensive or inappropriate.";
$replacement3 = "[blocked username]";
$review_recipient = preg_replace(Array($regex, $regex2),Array($replacement, $replacement3),$_POST['review_recipient']);
//$profile_id = intval($_POST['profile_id']); //dont know how you get this
$content = preg_replace(Array($regex, $regex2),Array($replacement, $replacement2),$_POST['review_content']);
//We check if all the fields are filled
if($_POST['review_content']!='') {
if($_POST['review_recipient']!='') {
$sql = "INSERT INTO ptb_reviews (id, from_user_id, from_guest, ip, to_user_id, content) VALUES (NULL, '-1', '".$review_recipient."', '" . $_SERVER['REMOTE_ADDR'] . "', '".$profile_id."', '".$content."');";
mysql_query($sql, $connection);
$_SESSION['message']="<div class=\"infobox-review-sent\"><strong>Thank You</strong> - Your review has been sent and is awaiting approval.</div><div class=\"infobox-close4\"></div>";
header("Location: {$_SERVER['HTTP_REFERER']}");
}
}
}
} } }
?>
【问题讨论】:
-
您遇到什么错误?另外,请使用 PDO mysql_query 命令已弃用。
-
$_SERVER['REMOTE_ADDR']是一个类似192.168.55.7的字符串。这对你来说看起来像一个整数吗? -
我没有收到任何错误,只是没有将任何内容插入到表中。 @Barmar,所以如果我将 ip 列更改为 varchar(48) 会起作用吗?
-
你甚至没有检查错误,你怎么知道?您需要检查
mysql_query的返回值,如果为false,则打印mysql_error()。您可以使用ip2long()将IP 地址转换为整数,也可以将其存储在char(15)中。 -
::1是localhost的 IPv6 地址。