除了WordPress插件中的IP外,如何在Session中保存用户的唯一ID?

时间:2020-06-28 作者:Başar Ballıöz

我想在我为wordpress开发的post-like插件中,从两台IP相同的计算机上得到喜欢。目前我的插件正在运行,但当我喜欢来自具有相同IP的设备的帖子时,另一个设备上会出现“不喜欢”按钮。我如何防止这种情况?

这是我的主要插件文件(.php):

<?php
/**
Plugin Name: LikeMyPost
Plugin URI:  https://github.com/basarballioz/Like-My-Post-WPPlugin
Description: It allows users to create a button that will make them like the posts, and monitor which tags are mostly liked.
Version:     1.0
Author:      Başar Ballıöz
Author URI:  https://github.com/basarballioz
License:     GPLv3
License URI: https://www.gnu.org/licenses/gpl-3.0.tr.html
 **/


//This will prevent public user to access your hidden files directly by using URL.
if (!defined(\'ABSPATH\') ) {
    exit;
}

// Load plugin files
require_once plugin_dir_path(__FILE__) . \'likemypost-widget.php\';           // plugin widget
require_once plugin_dir_path( __FILE__ ) . \'likemypost-admin.php\';          // admin menu entry and page content

class likeMyPost {
    
    //REGISTER STYLE AND JQUERY
    public function register_script() {
        //($handle, $src, $deps, $ver, $media) 
        wp_register_script(\'lmpScript\', plugins_url(\'js/lmpscript.js\', __FILE__), array(\'jquery\'), \'3.5.1\' );       
        wp_localize_script(\'lmpScript\', \'LMPajax\',
        array(\'ajax_url\' => admin_url(\'admin-ajax.php\'), \'nonce\' => wp_create_nonce(\'worldsBestPluginEver\'))); //SAFETY
    }

    public function loadScripts(){
        wp_enqueue_script(\'lmpScript\');
    }

    //ADD A LIKE BUTTON TO BOTTOM OF THE POSTS
    public function addLikeButton($content) {
        if (get_post_type() == is_singular()) {
            $getPost = \'<p class="getPostLiked" style="font-size: 1.1em; border-style: solid; border-width: thin; width: 200px"> You can, \';
        if ($this->alreadyLiked(get_the_ID())) {
            $getPost .= \'<br> <a style="color: pink;" data-event="dislike" data-post_id="\'.get_the_ID().\'" href="#/"> Dislike this post \';
        } else {
            $getPost .= \'<br> <a style="color: pink;" data-event="like" data-post_id="\'.get_the_ID().\'" href="#/"> Like this post! \';
        }
            $getPost .= \'</a><span class="count">\'.$this->likeCounter(get_the_ID());
            $getPost .= \' Likes <p></p></span></p>\';
            $content .= $getPost;
        }
        return $content;
    }

    //AJAX RESPONSE
    public function like() {
        //SAFETY
        check_ajax_referer(\'worldsBestPluginEver\', \'nonce\');
        
        $post_id = $_POST[\'post_id\'];
        $event = $_POST[\'event\'];
        if ($event == "like") {
            $this->likePost($post_id);
        } else {
            $this->dislikePost($post_id);
        }
        die();
    }

    //IP CONTROL FOR ALREADY LIKED
    public function alreadyLiked($post_id) {
        $user_IP = $_SERVER[\'REMOTE_ADDR\'];
        $meta_IP = get_post_meta($post_id, \'_likers_IP\');
        $likers_IP = $meta_IP[0];
        
        //SAFE ARRAYS (allows us to display the counter as zero when creating a new post - in order to prevent errors)
        if (!is_array($likers_IP)) {
            $likers_IP = array();
        }
        if (in_array($user_IP, $likers_IP)) {
            return true;                        //ALREADY LIKED (SHOW "like") button
        } else {
            return false;                       //SHOW "dislike" button
        }
    }

    //LIKING POSTS BY USING $POSTID
    public function likePost($post_id) {
        $likes_count = $this->likeCounter($post_id);
        $user_IP = $_SERVER[\'REMOTE_ADDR\'];
        $meta_IP = get_post_meta($post_id,\'_likers_IP\');
        $likers_IP = $meta_IP[0];

        //SAFE ARRAYS
        if (!is_array($likers_IP)) {
            $likers_IP = array();
        }
        $likers_IP[] = $user_IP;
        
        if (update_post_meta($post_id, \'_likes_count\', ++$likes_count)) {
            update_post_meta($post_id, \'_likers_IP\', $likers_IP);
            echo " ";
            echo "$likes_count Likes";
        } else {
            echo "Try again please...";
        }
    }

    //DISLIKING POSTS BY USING $POSTID
    public function dislikePost($post_id) {
        
        $likes_count = $this->likeCounter($post_id);
        $user_IP = $_SERVER[\'REMOTE_ADDR\'];
        $meta_IP = get_post_meta($post_id,\'_likers_IP\');
        $likers_IP = $meta_IP[0];

        //SAFE ARRAYS
        if (!is_array($likers_IP)) {
            $likers_IP = array();
        }
        if ($this->alreadyLiked($post_id)) {
            $key = array_search($user_IP,$likers_IP);
            unset($likers_IP[$key]);
        }
        if (update_post_meta($post_id, \'_likes_count\', --$likes_count)) {
            update_post_meta($post_id, \'_likers_IP\', $likers_IP);
            echo " ";
            echo "$likes_count Likes";
        } else {
            echo "Try again please...";
        }
    }

    public function likeCounter($post_id) {
        return get_post_meta($post_id, \'_likes_count\', true);
    }

    //HOOKS
    public function run() {      
        add_action(\'init\', array($this,\'register_script\'));
        add_action(\'wp_enqueue_scripts\', array($this,\'loadScripts\'));

        add_filter(\'the_content\', array($this, \'addLikeButton\' ));

        add_action(\'wp_ajax_nopriv_like\', array($this,\'like\'));
        add_action(\'wp_ajax_like\', array($this,\'like\'));
    }
}

//LIKE MY POST PLUGIN INITIALIZER
$plugin = new likeMyPost();  //Plugin object
$plugin->run();              //Call run function
这是我的jquery文件:

jQuery(document).ready(function () {
   jQuery(\'.getPostLiked a\').click(function () {
       
       let likeButton = jQuery(this);
       let post_id = likeButton.data(\'post_id\');
       let event = likeButton.data(\'event\');
       
       if (event == \'like\') {
          likeButton.text(\'Dislike this post!\');
          likeButton.data(\'event\',\'unlike\');
       } else {
          likeButton.text(\'Like this post!\');
          likeButton.data(\'event\',\'like\');
       }
       
       jQuery.ajax({
           type : \'post\',
           url : LMPajax.ajax_url,
           data : {
               action : \'like\',
               post_id : post_id,
               event : event,
               nonce : LMPajax.nonce
           },
           success : function (response) {   //WHEN ENDS
                        jQuery(\'.count\').text(response);
                     }
        });
    });
});

1 个回复
最合适的回答,由SO网友:mozboz 整理而成

这取决于您想要如何唯一地标识用户。一些唯一识别人员的选项可能是:

Wordpress用户ID。如果用户已登录,则使用其WP用户ID是一个不错的选择

$user_IP = $_SERVER[\'REMOTE_ADDR\'] . $_SERVER[\'HTTP_USER_AGENT\'];
这意味着每个like对于IP plus浏览器用户代理字符串都是唯一的,这应该可以解决您所述的问题。

相关推荐

将数组作为元值的GET_USERS

我的WordPress用户有一个自定义的生日元密钥(DD/MM/AAAA,例如2020年2月25日)。如果我想让所有用户今天(2002年2月25日)出生,我可以使用:$todays_birthday=\'25/02/\'; get_users(array(\'meta_compare\'=>\'like\', \'meta_key\'=>\'birthday\', \'meta_value\'=>$todays_birthday)); 它工作完美,返回所有今天出生的用户