2016-07-27 99 views
0

我已經爲wordpress中的header.php文件添加了一個五星評級系統的自定義腳本(當我可能應該有時,我沒有添加wp_enqueue_scripts掛鉤)。該javascript是可操作的,但我已經注意到在代碼中實現以防止來自相同IP地址的多票。我想查看是否有某種方法可以添加商店cookie功能來檢查選定者的IP到header.php文件或ratings.php文件中的自定義代碼中?如何將商店cookie功能添加到這個五星級評分系統?

我非常感謝您提供的任何建議!

這裏是header.php文件自定義代碼:

<?php wp_head(); ?> 
<script type="text/javascript"> 


$(document).ready(function() { 

    $('.rate_widget').each(function(i) { 
     var widget = this; 
     var out_data = { 
      widget_id : $(widget).attr('id'), 
      fetch: 1 
     }; 
     $.post(
      'http://localhost/url/wordpress/wp-content/themes/skt-magazine/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(
      'http://localhost/url/wordpress/wp-content/themes/skt-magazine/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 recorded (' + exact + ' rating)'); 
} 
// END FIRST THING 








</script> 

.rate_widget { 
     overflow: visible; 
     padding: 10px; 
     position: relative; 
     width:  180px; 
     height:  32px; 
    } 
    .ratings_stars { 
     background: url('http://localhost/url/wordpress/wp-content/uploads/2016/07/star_empty_hc.png') no-repeat; 
     float:  left; 
     height:  28px; 
     padding: 2px; 
     width:  32px; 
    } 
    .ratings_vote { 
     background: url('http://localhost/url/wordpress/wp-content/uploads/2016/07/star_full_hc2.png') no-repeat; 
    } 
    .ratings_over { 
     background: url('http://localhost/url/wordpress/wp-content/uploads/2016/07/star_highlight_hc.png') no-repeat; 
    } 
    .total_votes { 
     background: #eaeaea; 
     top: 58px; 
     left: 0; 
     padding: 5px; 
     position: absolute; 
    } 
    .movie_choice { 
     font: 10px verdana, sans-serif; 
     margin: 0 0 40px 0; 
     width: 180px; 
    } 
    h1 { 
     text-align: center; 
     width: 400px; 
     margin: 20px auto; 
    } 
</style> 

這裏是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() { 


preg_match('/star_([1-5]{1})/', $_POST['clicked_on'], $match); 
$vote = $match[1]; 

$ID = $this->widget_id; 

if($this->data[$ID]) { 
    $this->data[$ID]['number_votes'] += 1; 
    $this->data[$ID]['total_points'] += $vote; 
} 

else { 
    $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(); 
} 


} 
?> 
+0

我對你的問題弄得一點點,你在尋找如何獲得訪問者的IP? –

+0

Hi @HansStrausl,我正在嘗試添加一個cookie到腳本,以確保只有一個IP地址有一個投票的五星評級。隨着腳本的設置,現在任何人都可以從同一個IP地址進行多次投票。 – ThomE

回答

0

實現這樣的事情

if (document.cookie.indexOf("voted=") >= 0) { 
    // They've voted before. 
    canVote = false; 
} else { 
    // set a new cookie 
    expiry = new Date(); 
    expiry.setTime(date.getTime()+(1000*24*60*60*1000)); // 1000 days 

    // Date()'s toGMTSting() method will format the date correctly for a cookie 
    document.cookie = "voted=yes; expires=" + expiry.toGMTString(); 
    canVote = true; 
} 
+0

謝謝你分享你的時間給我一個很好的提示!我真誠地感謝你的迴應,並會測試這樣的事情! – ThomE