0

我有一个 MySQL 查询,它将表单的内容插入到我的数据库中。

除了插入这些值之外,我还想发现并插入用户 IP 地址。这是为了网站的保护。这样,如果用户不断发布有害内容,他们的 ip 可能会被阻止。

这是我的脚本。我正在尝试使用" . $_SERVER['REMOTE_ADDR'] . ",但它不适合我。有人可以告诉我这样做的方法。

我正在尝试将 IP 存储在我的表中名为“ip”的列中(int(10)属性:UNSIGNED NULL:无默认值:无)

更新:

当我将“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']}");
            }
        }

    }

} } }

?>
4

1 回答 1

0

检索时使用INET_ATON存储和 INET_NTOA:

<?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."', 'INET_ATON(" . $_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']}");
            }
        }

    }

} } }

?>

但是请注意,如果您使用的是 IPv6,这将不起作用。如果是这种情况,则需要 INET6_ATON 和 INET6_NTOA 函数。

于 2013-05-01T16:37:13.510 回答