0

I have created a simple like system where user can click on like button and the like count will increase by one . And if he has already liked the button and he like the button again then it will decrease the like count. Like facebook that of facebook.

here is a function that add or remove the like count. (this code will be called from wordpress ajax system)

function like_process(){
    global $wpdb;

    verify_nonce($_POST['nonce'],"like_system");
    $id = intval( $_POST['Post_ID'] );
    $like_the_post = intval( $_POST['like_the_post'] );

    $userid=get_current_user_id();

    $like_count = $wpdb ->get_row( "SELECT likes FROM {$wpdb->prefix}likes_counter WHERE post_id = '$id'" , ARRAY_N );

    $like_count=do_if_like_is_less_than_0($like_count);

    if( $like_count == null ) :
        $wpdb ->insert ('wp_likes_counter' ,array('post_id' => $id ,'likes' => 1,"ip_address"=>get_client_ip(),"user_id"=>$userid,"like_time"=>time()),array('%d' ,'%d','%s','%d',"%d"));
    die("added");
    else:
        if($like_the_post==true):
            $wpdb ->update ('wp_likes_counter' ,array('post_id' => $id ,'likes' => $like_count["0"]+1,"ip_address"=>get_client_ip(),"user_id"=>$userid,"like_time"=>time()),array('post_id' => $id ), array('%d' ,'%d','%s','%d',"%d"),array( '%d'));
        die("added");
        else:
            $wpdb ->update ('wp_likes_counter' ,array('post_id' => $id ,'likes' => $like_count["0"]-1,"ip_address"=>get_client_ip(),"user_id"=>$userid,"like_time"=>time()),array('post_id' => $id ), array('%d' ,'%d','%s','%d',"%d"),array( '%d' ) );
        die("removed");
        endif;
    endif;
die('nothing happened!!!');
}

Suppose we have like count 5 . If new user click like button the count will be 6. Again if he like button the count will be 5. So the count will be toggling between 5 and 6 as user click like button again and again.This is what we expect .

Like this :

enter image description here

Problem But if user click button very fast.say 3-4 times a second then the count will not toggle between 5 and 6. Sometime it goes 8 some time 3. Like this screenshot enter image description here WHy is this happening.

And we cannot even blame ajax as they are completely client side and client can do whatever they want isn't it? SO what is problem with php code..

Digged in for hours but still no solution as well as workaround :(

0

I think your problem is here:

$like_the_post = intval( $_POST['like_the_post'] );

You're relying on the user's browser's POST input to determine whether to increment or decrement the count. When a user quickly clicks the button, the AJAX call hasn't yet completed and changed the form input from true to false (or whatever's vice-versa). Additionally, this approach is liable to being spammed by a script repeatedly hitting your AJAX script with this value set to 'true' to artificially inflate the 'likes' on a post.

Since it looks like you're tracking the user_id when someone clicks the button, I'd suggest changing your AJAX code so that you first check to see if the user already has a record for that post_id - if not, increment the counter, if so decrement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.