duan6161 2012-10-31 16:02
浏览 54
已采纳

每次都会将插入的IP更改为MySQL数据库

I have a rating script that logs users' IPs so they can't rate the file more than once. However, every time I rate the file to test it, my $ip changes with the ip2long() function. What's up? Here's my code:

<?php

require('connect.php');

    $sheetID = $_GET["id"]; 
    $rating = $_GET["rating"];
$ip= $_SERVER['REMOTE_ADDR'];
$ip = ip2long($ip);

    $checkIPQuery = mysql_query("SELECT * FROM ratings WHERE sheetID = '$sheetID' and INET_NTOA(ip) = '$ip'");
    $doesIPExist = mysql_num_rows($checkIPQuery);
    if ($doesIPExist == 0) { 
        mysql_query("INSERT INTO ratings (sheetID, rating, ip) VALUES ($sheetID, $rating, $ip)"); 

        echo " $ip
<script>setTimeout(function(){location.href = '../sheets.php?id=$sheetID'},1);</script>"; 
    }
    else
        echo 'You have already rated this image!'; 

?>

And then for the check if the user has rated script:

$id = $_GET['id'];
$ip=$_SERVER['REMOTE_ADDR'];
$ip = ip2long($ip);

$checkIPQuery = mysql_query("SELECT * FROM ratings WHERE sheetID = '$id' and INET_NTOA(ip) = '$ip'");

$doesIPExist = mysql_num_rows($checkIPQuery);

Here's what I've done:

  • Changed the field to an unassigned INT when the $ip wouldn't go into a varchar(15)
  • I have added the ip2long() function because i changed the ip field to an INT
  • I then added the INET_NTOA(ip) to read the ip2long() INT

Any help would be appreciated. Thanks!

Edit: These scripts work fine if I set the $ip to 3 for example.

  • 写回答

1条回答 默认 最新

  • doufu3718 2012-10-31 17:11
    关注

    After reading the comments and a little research I found the answer and it is simple.

    On 32-bit systems ip2long() returns negative and positive integers but INET_NTOA() only works with positive integers. So you have two options to fix that:

    1. Change your PHP code: $ip = sprintf('%u', ip2long($_SERVER['REMOTE_ADDR']));
    2. Change your SQL query: ... AND ip = " . ip2long($_SERVER['REMOTE_ADDR']) . " ...

    You can verify this fact this way:

    $ip  = $_SERVER['REMOTE_ADDR'];
    $int = ip2long($ip);    
    var_dump($ip, $int, sprintf('%u', $int));
    

    This produces the following output:

    string '192.168.1.120' (length=13)
    int -1062731400
    string '3232235896' (length=10)
    

    Then go to MySQL:

    mysql> SELECT '192.168.1.120', INET_NTOA(-1062731400), INET_NTOA(3232235896)
    +---------------+------------------------+-----------------------+
    | 192.168.1.120 | INET_NTOA(-1062731400) | INET_NTOA(3232235896) |
    +---------------+------------------------+-----------------------+
    | 192.168.1.120 | NULL                   | 192.168.1.120         |
    +---------------+------------------------+-----------------------+
    1 row in set (0.00 sec)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥80 部署运行web自动化项目