2013-03-18 54 views
0

投票系統我做了一個代碼,生成投票系統和您的網站。他的出現如下圖所示:functions.php

exemple

我作了如下的代碼javascipt的:

jQuery(document).ready(function() { 

jQuery("a#up").click(function() { 
    jQuery('.rating').html(''); 
    jQuery('#loading').show(); 

    post_id = jQuery(this).data("post_id"); 

    jQuery.ajax({ 
     type: "post", 
     url: ajax_var.url, 
     data: "action=post_id&nonce=" + ajax_var.nonce + "&post_rating=up&post_id=" + post_id, 
     success: function(data) { 
      jQuery('#loading').hide(); 

      jQuery('.rating').html(data); 

     } 
    }); 

    return false; 
}); 

jQuery("a#down").click(function() { 

    post_id = jQuery(this).data("post_id"); 

    jQuery.ajax({ 
     type: "post", 
     url: ajax_var.url, 
     data: "action=post_id&nonce=" + ajax_var.nonce + "&post_rating=down&post_id=" + post_id, 
     success: function(data) { 
      jQuery('.rating').html(data); 

     } 
    }); 

    return false; 
}); 

}); 

而下面的PHP代碼在我的functions.php

<?php 

function add_rating_script() { 
    wp_enqueue_script('rating', get_stylesheet_directory_uri() . '/js/rating.js', array('jquery'), '', true); 
    wp_localize_script('rating', 'ajax_var', array('url' => admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('ajax-nonce'))); 
} 

function update_rating() { 

    // Check for nonce security 
    $nonce = $_POST['nonce']; 

    if (!wp_verify_nonce($nonce, 'ajax-nonce')) 
     die('Busted!'); 

    if (isset($_POST['post_rating'])) { 
     // Retrieve user IP address 
     $ip = $_SERVER['REMOTE_ADDR']; 
     $post_id = $_POST['post_id']; 

     // Get voters'IPs for the current post 
     $meta_IP = get_post_meta($post_id, "voted_IP"); 
     $voted_IP = $meta_IP[0]; 

     if (!is_array($voted_IP)) 
      $voted_IP = array(); 

     // Get votes count for the current post 
     $meta_count_up = get_post_meta($post_id, "votes_up", true); 
     $meta_count_down = get_post_meta($post_id, "votes_down", true); 

     // Use has already voted ? 
     if (!hasAlreadyVoted($post_id)) { 
      $voted_IP[$ip] = time(); 
      if ($_POST['post_rating'] == 'up') { 

       // Save IP and increase votes count 
       update_post_meta($post_id, "voted_IP", $voted_IP); 
       update_post_meta($post_id, "votes_up", ++$meta_count_up); 

       echo '<p class=\'message\'>Esse artigo já ajudou <span class=\'count\'>' . $meta_count_up . '</span> pessoas </p>'; 
      }elseif($_POST['post_rating'] == 'down'){ 
       update_post_meta($post_id, "voted_IP", $voted_IP); 
       update_post_meta($post_id, "votes_down", ++$meta_count_down); 

       echo '<p class=\'message\'>Obrigado pelo seu voto.</p>'; 
      } 

     } 
     else 
       echo '<p class=\'message\'>Você já enviou o seu voto.</p>'; 
    } 
    exit; 
    } 

    function hasAlreadyVoted($post_id) { 
    $timebeforerevote = 120; 

    $meta_IP = get_post_meta($post_id, "voted_IP"); 
    $voted_IP = $meta_IP[0]; 
    if (!is_array($voted_IP)) 
     $voted_IP = array(); 
    $ip = $_SERVER['REMOTE_ADDR']; 

    if (in_array($ip, array_keys($voted_IP))) { 
     $time = $voted_IP[$ip]; 
     $now = time(); 

     if (round(($now - $time)/60) > $timebeforerevote) 
      return false; 

     return true; 
    } 

    return false; 
} 

wp_localize_script('rating', 'ajax_var', array(
    'url' => admin_url('admin-ajax.php'), 
    'nonce' => wp_create_nonce('ajax-nonce') 
)); 

add_action('wp_enqueue_scripts', 'add_rating_script'); 
add_action('wp_ajax_post_id', 'update_rating'); 
add_action('wp_ajax_nopriv_post_id', 'update_rating'); 

問題是爲了防止用戶在同一篇文章中多次投票,我保存了它的IP並做了一個比較,但我相信隨着時間的推移將會留下Word表格新聞充電,超級慢。有誰知道我優化此代碼的某些內容?

回答

0

優化的一個竅門可能是在投票後設置cookie並將其作爲第一道防線。這會在很多用例中阻止一些回調數據庫的調用。如果沒有設置cookie,只需在表格中查找IP地址,以確定它們是否已投票。

由於用戶可以切換瀏覽器,清除cookie等,因此顯然,僅使用cookie的方法將無法工作 - 對於這些用戶,您將像當前一樣查看其IP(儘管仍然可能存在規避這一點,而且如果他們共享互聯網連接/ IP地址 - 即來自同一星巴克Wifi連接的投票,您可能會阻止合法用戶投票。

至少通過首先檢查cookie,您可以主動防止大量潛在不必要的數據庫事務。

+0

確實是一個好主意! 我不想要很多安全性的東西,只是想阻止一個人反覆點擊按鈕。 如果我停止它,直到它清除緩存,並且已經足夠。 – Renato 2013-03-18 22:59:32