如何在这个五星评级系统中添加商店cookie功能?

I have added a custom script for a five star rating system to my header.php file in wordpress (I did not add a wp_enqueue_scripts hook when I probably should have). The javascript is operational but I have noting implemented in the code to prevent multiple votes from the same IP Address. I wanted to see if there is some way I can add a store cookie function to check the IP of the voters to the custom code in the header.php file or to the ratings.php file?

I greatly appreciate any advice you can provide!

Here is the custom code from the header.php file:

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

And here is the ratings.php file that I had mentioned:

<?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();
}


}
?>

Implement something like this

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