2016-07-27 17 views
0

Ich habe meiner header.php-Datei in Wordpress ein benutzerdefiniertes Skript für ein Fünf-Sterne-Bewertungssystem hinzugefügt (ich habe keinen wp_enqueue_scripts-Hook hinzugefügt, wenn ich wahrscheinlich hätte). Das Javascript funktioniert, aber ich habe bemerkt, dass es im Code implementiert wurde, um mehrere Stimmen von derselben IP-Adresse zu verhindern. Ich wollte sehen, ob es eine Möglichkeit gibt, eine Store-Cookie-Funktion hinzuzufügen, um die IP der Wähler auf den benutzerdefinierten Code in der Datei header.php oder in der Datei ratings.php zu überprüfen.Wie kann ich diesem Fünf-Sterne-Bewertungssystem eine Cookie-Funktion hinzufügen?

Ich schätze jede Beratung sehr, die Sie zur Verfügung stellen können! Hier

ist der benutzerdefinierte Code aus der Datei 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> 

Und hier ist die ratings.php Datei, die ich hatte erwähnt:

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

ich ein wenig durch Ihre Frage verwirrt bin, suchen Sie, wie Sie die IP-Adresse eines Besuchers zu bekommen? –

+0

Hi @HansStrausl, ich versuche, dem Skript einen Cookie hinzuzufügen, um sicherzustellen, dass nur eine IP-Adresse eine Stimme für die Fünf-Sterne-Bewertung hat. Mit der Art, wie das Skript eingerichtet ist, kann jeder von der gleichen IP-Adresse beliebig oft abstimmen. – ThomE

Antwort

0

Implementieren so etwas wie dieses

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

Vielen Dank für Ihre Zeit, um mir einen guten Tipp zu geben! Ich schätze Ihre Antwort sehr und werde so etwas testen! – ThomE