dpbf62565 2013-06-27 08:58
浏览 167

如何确保用户只能在24小时内投票一次

I am building a feature that allows a user to vote only once in every 24 hours.

Here is my database structure

 id ip        timestamp           userid
  9 127.0.0.1 2013-06-27 16:52:49 35

I used this function to get the user client up address, i am in localhost environment and it keep giving me localhost ip 127.0.0.1 instead of my public ip. Is there anything wrong with it?

function get_client_ip() {
    $ipaddress = '';
    if (getenv('HTTP_CLIENT_IP'))
        $ipaddress = getenv('HTTP_CLIENT_IP');
    else if(getenv('HTTP_X_FORWARDED_FOR'))
        $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
    else if(getenv('HTTP_X_FORWARDED'))
        $ipaddress = getenv('HTTP_X_FORWARDED');
    else if(getenv('HTTP_FORWARDED_FOR'))
        $ipaddress = getenv('HTTP_FORWARDED_FOR');
    else if(getenv('HTTP_FORWARDED'))
        $ipaddress = getenv('HTTP_FORWARDED');
    else if(getenv('REMOTE_ADDR'))
        $ipaddress = getenv('REMOTE_ADDR');
    else
        $ipaddress = 'UNKNOWN';

    return $ipaddress;
}

Is there something wrong my logic for this feature, my idea is

1) first query tabel to see if ip for the user is persisted. If so, he/she voted 2) Check timestamp of vote against current timestamp, if his voting time is way before 24 hours, then allow him to vote again.

My concern is not with the timestamp, but with the ipaddress. I do not know if the function i built is good enough to get his ipaddress, even if he is behind proxy.

Advices appreciated. thanks

Cheers

  • 写回答

2条回答 默认 最新

  • dpjj4763 2013-06-27 09:03
    关注

    Why you don't set a cookie? You set a 24h expiring time. I know thats not 100% secure but I can easily reconnect to internet to change my ip so ip check is "insecure" too.

    if $_COOKIE["alreadyVoted"] throw new Exception(...)
    

    http://php.net/manual/en/features.cookies.php

    http://www.php.net/manual/en/function.setcookie.php

    Hope this helps.

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么