检索用户的IP地址并将其存储在数据库表中?

时间:2013-05-01 16:30:18

标签: mysql ip

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

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

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

我正在尝试将IP存储在我的表中名为'ip'的列中(int(10)Atributes: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']}");
            }
        }

    }

} } }

?>

1 个答案:

答案 0 :(得分: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功能。