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

问题描述 投票:0回答:1

我正在尝试开发一个仅供我个人使用的Php Photo Gallery,并且我使用我在网上找到的修改过的脚本放了一个Php System Rating ...一切正常,除了一件事,我不能阻止用户在同一个帖子中发布几张票天!我希望用户投票照片(几张照片),但在同一天投票一次(每张照片一票)...我在这里发布了我修改过的脚本。

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)' );
}

我试图在ratings.php中实现IP机制,如下所示

<?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

php jquery gallery photo rating
1个回答
2
投票

最简单的方法是在数据表中通知谁投票和哪天。

例如:Toto在2014-07-04投票,所以他今天不能投票两次。

在数据表用户中,您可以添加列日期以通知投票的最后一天。

你可以使用cookies,但它非常难看!

© www.soinside.com 2019 - 2024. All rights reserved.