Ajax Voting Script - Vote every 12 hours

Place for comments, problems, questions, or any issue related to the JavaScript / PHP scripts from this site.
Nike555
Posts:12

Ajax Voting Script - Vote every 12 hours

It's possible to make multivote for 1 user, in the Ajax Voting Script? From: https://coursesweb.net/php-mysql/voting- ... pt-ajax_s2
For example 1 user can vote any 12 hours.

Admin Posts:805
Hello,
If you want the user to can vote every 12 hours, download and use the php files from this attachment:
Attachments
voting_12_hours.zip
(4.27KiB)Downloaded 457 times

Nike555 Posts:12
Script succesufull runned, but when i add vote, he don't add new row in votusers.
And how i see you are changed sctructure of table "voteusers", you are added new column "day"
this is have structure like old column "nextv" ? int(10)?

Admin Posts:805
With the changes I made in the code above, the script will use 2 tables: 'votusers_1' for the first 12 hours, and 'votusers_2' for the last 12 hours.
So, you need to use this code for "create_tables.php":

Code: Select all

<?php
include('voting.php');

// if SVOTING is 'mysql', create tables, else, output mesage to set 'mysql'
if(SVOTING == 'mysql') {
  $sqlc['votusers_1'] = "CREATE TABLE `votusers_1` (`day` INT(2), `voter` VARCHAR(15), `item` VARCHAR(200) NOT NULL DEFAULT '') CHARACTER SET utf8 COLLATE utf8_general_ci";

  $sqlc['votusers_2'] = "CREATE TABLE `votusers_2` (`day` INT(2), `voter` VARCHAR(15), `item` VARCHAR(200) NOT NULL DEFAULT '') CHARACTER SET utf8 COLLATE utf8_general_ci";

  // Table to store items that are voted
  $sqlc[$obVot->votitems] = "CREATE TABLE `$obVot->votitems` (`item` VARCHAR(200) PRIMARY KEY NOT NULL DEFAULT '', `vote` INT(10) NOT NULL DEFAULT 0, `nvotes` INT(9) NOT NULL DEFAULT 1) CHARACTER SET utf8 COLLATE utf8_general_ci";

  // traverse the $sqlc array, and calls the method to create the tables
  foreach($sqlc AS $tab=>$sql) {
    if($obVot->sqlExecute($sql) !== false) echo "<h4>The '$tab' table was created</h4>";
  }
}
else echo 'Set "mysql" value to SVOTING, in voting.php';  
- But, another way is to use the files from the voting_up_down_set_time.zip, in the archive with the script. Then, set the value of the NEXTV constant (line 17) in the new "voting.php" file.
For example, for the user to can vote over 12 hours, set:

Code: Select all

define('NEXTV', strtotime('+12 hours'));  

Nike555 Posts:12
I use only script with time (voting_up_down_set_time.zip), i need changes in this script)

And if i use script with time try to add this 2 new tables?

Admin Posts:805
If you want to use the code from voting_up_down_set_time.zip, you not need to create those 2 tables, just use the php files from voting_up_down_set_time.zip (including "create_tables.php"). Then set the value for NEXTV constata as showed above.

Nike555 Posts:12
Thanks, but it's more comformable if i can use only 1 table "voteusers".

1 question: where is code control of IP (if ip voted in this 12 hours or no) ?
I want to make this control and for username (1 vote of this username in 12 hours).

I thought this secure is in this function, but not:

Code: Select all

// returns from mysql an array with items voted by the user today
  protected function votstdyDb() {
    $votstdy = array();
    $resql = $this->sqlExecute("SELECT `item` FROM `$this->votusers` WHERE `nextv`>$this->time AND `voter`='$this->voter' AND `username`='$this->voterUser'");
    if($this->affected_rows > 0) {
      for($i=0; $i<$this->affected_rows; $i++) {
        $votstdy[] = $resql[$i]['item'];
      }
    }

    return $votstdy;
  } 
Thanks

Admin Posts:805
The "nextv" column contains the time after which the user can vote again.
This time is defined in the NEXTV constant, in voting.php:

Code: Select all

define('NEXTV', strtotime('+12 hours'));  // LINE 17, TO CAN VOTE EVERY 12 HOURS 
So, this select returns all the rows with items voted by the ip /user in the specified time interval defined in NEXTV constant.
If you want to select the username, just add it in Select query:

Code: Select all

"SELECT username, `item` FROM `$this->votusers` WHERE `nextv`>$this->time AND `voter`='$this->voter' AND `username`='$this->voterUser'" 
Or, you can make other select to return all the users that voted in the NEXTV time, and cannot vote again till that time expires:

Code: Select all

"SELECT username FROM `$this->votusers` WHERE `nextv`>$this->time" 
In the setVotDb() function, this line of code deletes the rows with expired time restriction (after wich the user can vote again), to keep only the needed data that tell the script which items cannot be voted in the current time.

Code: Select all

$this->sqlExecute("DELETE FROM `$this->votusers` WHERE `nextv`<$this->time");