dongliyu3278 2014-09-17 14:12
浏览 39
已采纳

照片库的Php系统评级,每张照片允许1票/天

I'm trying to develop a Php Photo Gallery only for my personal use and I put a Php System Rating using a modified script that I found on the web... all works fine except for one thing, I cannot stop users from posting several votes in the same day! I'd like that users vote the photos (several photos as well) but voting one time in the same day (one vote for each photo)... I post here the script that I have modified.

ratings.php:

<?php
  $rating = new ratings($_POST['widget_id']);
  isset($_POST['fetch']) ? $rating->get_ratings() : $rating->vote();
  class ratings {
    var $data_file = './ratings.data.txt';
    private $widget_id;
    private $data = array();
    function __construct($wid) {
      $this->widget_id = $wid;
      $all = file_get_contents($this->data_file);
      if ($all) {
        $this->data = unserialize($all);
      }
    }
    public function get_ratings() {
      if ($this->data[$this->widget_id]) {
        echo json_encode($this->data[$this->widget_id]);
      } else {
        $data['widget_id'] = $this->widget_id;
        $data['number_votes'] = 0;
        $data['total_points'] = 0;
        $data['dec_avg'] = 0;
        $data['whole_avg'] = 0;
        echo json_encode($data);
      }
    }
    public function vote() {
      # Get the value of the vote
      preg_match('/star_([1-5]{1})/', $_POST['clicked_on'], $match);
      $vote = $match[1];
      $ID = $this->widget_id;
      # Update the record if it exists
      if ($this->data[$ID]) {
        $this->data[$ID]['number_votes'] += 1;
        $this->data[$ID]['total_points'] += $vote;
      } else {  # Create a new one if it doesn't
        $this->data[$ID]['number_votes'] = 1;
        $this->data[$ID]['total_points'] = $vote;
      }
      $this->data[$ID]['dec_avg'] = round($this->data[$ID]['total_points'] / $this->data[$ID]['number_votes'], 1);
      $this->data[$ID]['whole_avg'] = round($this->data[$ID]['dec_avg']);
      file_put_contents($this->data_file, serialize($this->data));
      $this->get_ratings();
    }
    # ---
    # end class
  }
?>

ratings.js:

$(document).ready(function() {
  $('.rate_widget').each(function(i) {
    var widget = this;
    var out_data = {
    widget_id : $(widget).attr('id'),
    fetch: 1
  };
  $.post(
  'ratings/ratings.php',
  out_data,
  function(INFO) {
    $(widget).data('fsr', INFO);
    set_votes(widget);
  },
  'json'
  );
  });
  $('.ratings_stars').hover(
  function() {
    $(this).prevAll().andSelf().addClass('ratings_over');
    $(this).nextAll().removeClass('ratings_vote'); 
  },
  function() {
    $(this).prevAll().andSelf().removeClass('ratings_over');
    set_votes($(this).parent());
    }
  );
  $('.ratings_stars').bind('click', function() {
    var star = this;
    var widget = $(this).parent();
    var clicked_data = {
    clicked_on : $(star).attr('class'),
    widget_id : $(star).parent().attr('id')
  };
  $.post(
  'ratings/ratings.php',
  clicked_data,
  function(INFO) {
  widget.data('fsr', INFO);
  set_votes(widget);
  },
  'json'
  ); 
  });
});
function set_votes(widget) {
  var avg = $(widget).data('fsr').whole_avg;
  var votes = $(widget).data('fsr').number_votes;
  var exact = $(widget).data('fsr').dec_avg;
  window.console && console.log('and now in set_votes, it thinks the fsr is ' + $(widget).data('fsr').number_votes);
  $(widget).find('.star_' + avg).prevAll().andSelf().addClass('ratings_vote');
  $(widget).find('.star_' + avg).nextAll().removeClass('ratings_vote'); 
  $(widget).find('.total_votes').text( votes + ' votes (' + exact + ' rating)' );
}

I tried to implement IP mechanism in ratings.php as below without lucky

<?php
  $rating = new ratings($_POST['widget_id']);
  isset($_POST['fetch']) ? $rating->get_ratings() : $rating->vote();
  class ratings {
    var $data_file = './ratings.data.txt';
    private $widget_id;
    private $data = array();
    function __construct($wid) {
      $this->widget_id = $wid;
      $all = file_get_contents($this->data_file);
      if ($all) {
        $this->data = unserialize($all);
      }
    }
    public function get_ratings() {
      if ($this->data[$this->widget_id]) {
        echo json_encode($this->data[$this->widget_id]);
      } else {
        $data['widget_id'] = $this->widget_id;
        $data['number_votes'] = 0;
        $data['total_points'] = 0;
        $data['dec_avg'] = 0;
        $data['whole_avg'] = 0;
        echo json_encode($data);
      }
    }
    public function vote() {
      # Get the value of the vote
      preg_match('/star_([1-5]{1})/', $_POST['clicked_on'], $match);
      $vote = $match[1];
      $ID = $this->widget_id;
      # Update the record if it exists
      if ($this->data[$ID]) {
        $this->data[$ID]['number_votes'] += 1;
        $this->data[$ID]['total_points'] += $vote;
        $this->data[$ID]['remote_ip'] = $_SERVER['REMOTE_ADDR'];
      } else {  # Create a new one if it doesn't
        $this->data[$ID]['number_votes'] = 1;
        $this->data[$ID]['total_points'] = $vote;
        $this->data[$ID]['remote_ip'] = $_SERVER['REMOTE_ADDR'];
      }
      if ($this->data[$ID]['remote_ip'] != $_SERVER['REMOTE_ADDR']) {
        $this->data[$ID]['dec_avg'] = round($this->data[$ID]['total_points'] / $this->data[$ID]['number_votes'], 1);
        $this->data[$ID]['whole_avg'] = round($this->data[$ID]['dec_avg']);
        file_put_contents($this->data_file, serialize($this->data));
        $this->get_ratings();
      }
    }
    # ---
    # end class
  }
?>

my web gallery

  • 写回答

1条回答 默认 最新

  • douyu9433 2014-09-17 14:16
    关注

    The simplest way is to notify in a data table who vote and which day.

    For example : Toto vote on 2014-07-04, so he can't vote twice today.

    In data table user you add a colum date to notify the last day of vote.

    You can use cookies but it's very very ugly !

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错