Jump to content

[SOLVED] Refresh page cause double vote/rate


eevan79

Recommended Posts

I have problem with code where users can rate random photos. But when user already rate photo he can refresh page and rate again for same photo twice.

For example index.php shows 2 photos. User can rate one photo:

.../index.php?vote=1,2 //rate first photo

or

.../index.php?vote=2,1 //rate second photo

 

Here is part of the code for vote:

				require_once('mysqlvalues.inc.php');
			require_once('mysqlfunc.inc.php');

			open_conn();

			$res=mysql_query("SELECT ID, Name, Thumbnail, LinkText, LinkUrl FROM Models WHERE Status='1' ORDER BY RAND() LIMIT 2");

			while($row=mysql_fetch_assoc($res)) {

				$ID[] = $row[iD];
				$Thumbnail[] = $row[Thumbnail];
				$Name[] = $row[Name];

				if($row['LinkText'] != '' && $row['LinkUrl'] != '') {$Url[]="<br><br><a href=\"$row[LinkUrl]\">$row[LinkText]</a><br><br>";}else{$Url[]='';}

			}

				echo"<td valign=\"top\">
								<a href=\"index.php?vote=$ID[0],$ID[1]\"><img src=\"images/uploads/$Thumbnail[0]\" alt=\"$Name[0]\"  title=\"$Name[0]\" border=\"0\" width=\"320\" height=\"320\" /></a>
								<br /><strong>$Name[0]</strong> $Url[0]
							</td>
							<td align=\"center\">
								<img src=\"images/vs.jpg\" alt=\"vs\" title=\"vs\"/>
							</td>
							<td valign=\"top\">
								<a href=\"index.php?vote=$ID[1],$ID[0]\"><img src=\"images/uploads/$Thumbnail[1]\" alt=\"$Name[1]\"  title=\"$Name[1]\" border=\"0\" width=\"320\" height=\"320\" /></a>
								<br /><strong>$Name[1]</strong> $Url[1]
							</td>";

			close_conn();

			?>		

How can I prevent refresh page (or avoid somehow) so users cant vote infinity times one photo?

 

Regards,

Ivan

 

[attachment deleted by admin]

Link to comment
Share on other sites

Create a session variable to store the id photo when a user votes for it.

 

Then, each time a user votes, check that the photo being voted upon isn't in the session already.

The user will be unable to vote for the same photo again until they begin a new session.

 

Something like this:

if(!isset($_SESSION["vote"]["id_photo"])){
  // your vote save code
  
  // save id photo into the session
  $_SESSION["vote"]["id_photo"] =1;
}

Link to comment
Share on other sites

cbolson,

thanks for reply.

 

...yes that can be solution, but what happen when stored ID photo displayed again (images are displayed by random).

 

Here is how it works:

when user visit website (index.php) he can vote for one of two images. One image is "winner", second is "looser". When he vote for one image (example: index.php?vote=1,2) page reload with new vote and in left corner display result of previous vote. So sometimes it's possible to display same image from previous vote.

 

Note: I am not familiar with .php, but know Java, html/css and editing some .php code (using php tutorial). This "photobattle" script is some open source example script which i have edited a lot.

Link to comment
Share on other sites

So, if the user can't vote for the same photo more than once, surely the best option would be to not show it again, wouldn't it?

 

If this is the case you would need to condition the database query to not select the photos whose id is in the session variable array.

 

To achieve this you can loop through the ids in the array to add a condition for your database query something like this:

$sql_condition="";
foreach($_SESSSION["vote"] AS $id_photo){
$sql_condition.=" AND ID!=$id_photo";
}
$res=mysql_query("SELECT ID, Name, Thumbnail, LinkText, LinkUrl FROM Models WHERE Status='1' ".$sql_condition." ORDER BY RAND() LIMIT 2");

Chris

Link to comment
Share on other sites

I tried but it doesnt work. Also tried to read some tutorials, but w/o result.  :confused:

 

I need to start session with 2 IDs ($ID[0] and $ID[1]) where $ID[0] is first image, and $ID[1] is second image.

When user refresh page, he cant vote again.

 

Example: user vote (click) for image[1] than image[1] gain +1, and image[2] gain -1. When he refresh page image[1] again gain +1, and image[2] gain -1. So he can ifinity refresh page. It's really annoying.

 

Please look at attached file (index.php, first post) and help me to implement session to prevent same vote when page is refreshed. Code where user vote begin @91 line.

 

cbolson, your solution may work but I do not know if it is properly implemented.

Thanks.

Link to comment
Share on other sites

OK, I have taken a quick look at your file and made a few changes.

You should be able to find my code as I have added a comment before starting "// chris - "

 

Basically I have used session_start() at the top of the page to start the php session (without this it will never work)

When the user votes a session variable is defined with the id of the photo.

This session variable is used to prevent the mysql_query() that updates the votes from being exectued more than once if the page is reloaded.

It is also used to stop the photos from being shown again once they have been voted.

 

Clearly I haven't tested this code as there is a lot of other stuff in there which relates to your other database stuff.

Let me know how it goes ;)

 

Chris

 

[attachment deleted by admin]

Link to comment
Share on other sites

are you getting a blank page on page load - ie can you even see the photos for voting?

A blank page normally means that there is some php error.

 

It sounds like you server has php error reporting turned off.

 

If you add these lines at the beginning of your page you should start to see some useful php errors:

 

error_reporting(E_ALL ^ E_NOTICE);
ini_set("display_errors", 1); 

 

Chris

Link to comment
Share on other sites

Now I noticed that it was partially resolved...Only second refresh works, but first refresh (after vote) still updating queries...Not big deal but maybe it can be fixed ...  :confused:

 

It's strange, but now it working fine (i didnt touch anything). But on previous test update queries after first refresh...(thats why i never understand .php) :)

 

I mark this "solved".  :shy:

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.