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 !

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

报告相同问题?

悬赏问题

  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?