Like/Dislike-Script only once per user.

#1

Hello,

I use this Ajax Like/Dislike Script: http://www.phpkobo.com/ajax_likes.php
My users can register to my wordpress blog and now it would be great if every registered user can vote only once (i already realised that unregistered user don’t see the like/dislike script).

Is there an easy way / script to realize that?

thank you

#2

look at jQuery Cookie

#3

but every cookie can be deleted by the user, right?
My idea was to save variables for every post and user and just show him the like/dislike poll if the logged in user hasn’t already voted

#4

the most convenient way is to use post meta and store the names of the users (or their ids) in an array

#5

Well you can do with PHP / WP

In the header.php, set a variable e.g.

$can_vote = '1';

Before the HTML for the vote, check the variable:

<?php
if($variable == '1') {
 // display vote
} else {
 // already voted
}
?>

Now, the fun part, create a new user field in WP and set the default to be 1

When they have voted, you need to change the variable to 0 (or something else, and also edit the user field to be the same.

The WP codex will show up ways to create user fields, but it’s not the simplest and cleanest method to do this.

#6

I have done that in WordPress some time ago when I was developing a 9GAG theme for wp, I solved it like this:

In user meta, stor array of post IDs user liked/disliked
In post meta, store number of likes

$On init:

Get array of post IDs user liked/disliked, compare them in the loop with the id of the post and apply proper styling

$On like click:

Get post ID, compare it to the user liked/disliked array-s:

$$If user already liked: take away one like from post meta, take away post ID from liked array in user meta, apply proper styling

$$If user already disliked: add two points to post meta ( one for removing unlike and one for adding like ), add post id to user meta liked array, remove post id from user meta disliked id

$$If user did not like/dislike: add post id to user meta liked array, add one like to post meta

$On dislike click:

Get post ID, compare it to the user liked/disliked array-s:

$$If user already liked: remove two points from post meta (one for new dislike and one for like removal), remove post id from user meta liked array, add post id to user meta disliked array

$$If user already disliked: add one point to post meta, remove post id from user meta disliked array

$$If user did not like/dislike: remove one point from post meta, add post id to user meta disliek array

Sorry for this sausage :stuck_out_tongue:

#7

ok thank you.
my php skills are not so good.

do you know a specific tutorial? I haven’t found any.

#8
LovelessDesign said

ok thank you.
my php skills are not so good.

do you know a specific tutorial? I haven’t found any.

Unfortunately no, sorry, I did not follow the tutorial, I only used the knowledge I had + logic.
#9

@gareth, you should be using booleans or integers, not strings.

@loveless, why not just use a plugin like zilla Likes?

#10

Zilla Likes also just uses Cookies to prevent double-votes. Users just have to delete cookies to vote again.

I now want to use a facebook/twitter login plugin

Users have to login with their facebook/twitter account to see the voting.

But does anyone know a detailed instruction on how to realize that every facebook/twitter account can vote only once? The HTML-Code that displays the Vote-Button could just disappear if this fb/tw account has already voted.

would be great. thank you guys.

#11

I’ve tried it on my own now and I’m close to a solution. Maybe someone could help me with the final step? Maybe someone who could need it too?

So, I have a FB Login Plugin ( http://www.sociable.es/facebook-connect-wordpress-plugin-screenshots/ )
And an Ajax Like/Dislike Script ( http://www.phpkobo.com/doc.php?d=install&p=AL201-102 )

You could download all the important files with all my changes here:
http://markenkampf.com/likedislike.zip

Logged in Users (via facebook) should be able to vote only once,
That’s why I’m writing the user id + page id in a sql table. If the same user id wants to vote the same page id, again it doesn’t work.

My only problem that needs to be solved:
If the user clears the cookies after his first vote, he could vote a second time. But NOT a third or fourth time. The problem is that the PHP code doesn’t wait until the MYSQL request is finished.

I tried to build workaround in the JS file, but I’m not very good in it and so the code looks too complex for me.

Thanks a lot.

(it also needs to be fixed that a user who isn’t logged in, can’t vote. but that’s no problem for me)

#12

Does anyone have a solution for this?
The if ( $voteaccepted == ‘yes’ ) { at the bottom doesn’t work because SQL is too slow.

if ($_REQUEST['buttonclick'] == "yes" ) {
	$con = mysql_connect('mysql5.loveless.businesspage.at', 'db381825', 'rBTFTGHv');
	if (!$con)
	{
	  die('Could not connect: ' . mysql_error());
	}
	mysql_select_db("db381825", $con);

	$userid = $_REQUEST["userid"];
	$pageid = $_REQUEST["pageid"];

	$einlesen = mysql_query("SELECT COUNT(*) FROM a1wbabs_votelock WHERE wp_id=$userid AND page_id=$pageid");
	if(mysql_result($einlesen,0)<1) { 
		$voteaccepted = "yes";
		mysql_query("INSERT INTO a1wbabs_votelock (wp_id, page_id)
		VALUES ($userid, $pageid)");
	} else {
		$voteaccepted = "no";
	}	
	mysql_close($con);			
}
// LLD END

//-- add votes
if ( $voteaccepted == 'yes' ) {
//THIS SHOULD ONLY HAPPEN ONCE but it's possible to get there a second time because the variable $voteaccepted is not set after the first time (SQL is too slow)
}
#13

I don’t think your $voteaccepted variable is in scope at the point at which you test it based on this code. You probably just need to declare/initiaize the variable prior to the first if statement.

MySQL queries are synchronous so there shouldn’t be any race condition (which I assume is what you mean by SQL being too “slow”) here.

I’d recommend using booleans or at least constants in your code, rather than strings.

Also, if this is for WordPress, you should definitely be using the WordPress database functionality.

#14

Thank you for your help.

Hm I can’t get it to work. I tried to put the code everywhere in the php file.
For some reason I can’t use global variables in this file - The Like Script would work anymore.

Additionally, for some reason booleans, as you mentioned, doesn’t work. Maybe because of this code in the JS file:

 $.post( this.url_server,
			{ cmd: "vote", tid:this.tid, vote:v, userid:this.userid, pageid:this.pageid, buttonclick:this.buttonclick, voteaccepted:this.voteaccepted },
			function(data) { 

I’m trying to fix the problem for the whole day now. I think it’s too hard for me.
I have problems with the understanding of the order the code runs and there is much code I haven’t seen before.

#15

I haven’t changed it to the wpdb yet because it’s in a folder in the wordpress root and not really in wordpress. I want to fix the other issue before I learn the use of the wpdb function. Or Do you think my Main issue is because I don’t use wpdb?

Thank you

#16

I’ve Finally Found the error!
It had to be ($einlesen,0)<0) instead of ($einlesen,0)<1)

Now, I have another question (jquery).
I have two jquery functions .

$(document).ready(function() {
	$( '.ld-container' ).each( function(){
		var buttonclick = 'no';
		var voteaccepted = 'yes';
		var obj = new CAjaxLikes( this,buttonclick,voteaccepted ); 
		obj.run();
	});
	$( '.ld-container .button' ).click( function(){
		var buttonclick = 'yes';
		var voteaccepted = 'yes';
		var obj = new CAjaxLikes( this,buttonclick,voteaccepted ); 
		obj.run();
	});
});

The Problem is that if I click one of the Submit Buttons, the each function AND the click function run.

But I want the each function to only run on page load. If a Submit button is clicked, only the click function should run. This is the HTML:

pageid=<?php the_ID(); ?> action='http://xxx.de/poll/ajax_likes.php'> pageid=<?php the_ID(); ?> team=1 action='http://xxx.de/poll/ajax_likes.php'> Jetzt abstimmen pageid=<?php the_ID(); ?> team=2 action='http://xxx.de/poll/ajax_likes.php'>
#17

EDIT: Seems I didn’t actually read the thread post! :doh:

  • Ed
#18

Maybe it’s not the best solutions with the action attribute in the div and in the buttons.
But the plugin was built in that way and I don’t want to change it if not necessary.

So is there a way to don’t run the each function when a submit buttpn is clicked?

#19

@joshipucher can you add again the ajax likes script on your server ( http://markenkampf.com/likedislike.zip ) ? I would like to test if i can save links when i click LIKE/DISLIKE…