Adding login username in the Ajax Voting Script

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

Adding login username in the Ajax Voting Script

It'si it possible to add to the table of voters (votusers) login username of voter, when user vote? In the Ajax Voting Script: https://coursesweb.net/php-mysql/voting- ... pt-ajax_s2
Login i get with function: $fgmembersite->UserFullName();.

I use the code from "voting_up_down_set_time.zip" Thanks, and i made some changes in this script for mine:
prntscr.com/9azdum

I would like to set in column of this table "username", login of this account, variable:

Code: Select all

$fgmembersite->UserFullName()
Can you help me with this changes?
Thanks

Admin Posts: 805
You just have to add in the VOTER constant (in voting.php) the login name returned by your other script.
In the voting.php file there is this code:

Code: Select all

// If you want than only the logged users to can vote the element(s) on page, sets USRVOTE to 0
// And sets $_SESSION['username'] with the session that your script uses to keep logged users
define('USRVOTE', 1);
if(USRVOTE !== 1) {
  if(!isset($_SESSION)) session_start();
  if(isset($_SESSION['username'])) define('VOTER', $_SESSION['username']);
}
So, you can use it like this (replace $_SESSION['username'] with the session in which the other script stores the login username):

Code: Select all

// If you want than only the logged users to can vote the element(s) on page, sets USRVOTE to 0
// And sets $_SESSION['username'] with the session that your script uses to keep logged users
define('USRVOTE', 0);
if(USRVOTE !== 1) {
  if(!isset($_SESSION)) session_start();
  if(isset($_SESSION['username'])) define('VOTER', $_SESSION['username']);
  // or this, if you not have a session with username
  else if(isset($fgmembersite)) define('VOTER', $fgmembersite->UserFullName());
} 
In this way you not need to make changes into the class.voting.php, nor to create another column into the mysql table; the username will be stored in the "voter" column.

Nike555 Posts: 12
I tryed to set in column "username" variable "VOTER", not add nothing in the table(with your script from last post).
I tryed

Code: Select all

	session_start();
	  $username = $_SESSION['username'];
and use in insert $username, not add nothing in the table.

With old script (defined VOTER), when i vote, add new row, but in this column is NULL.

Admin Posts: 805
Set 0 to the USRVOTE constant and define that username to the VOTER constant, or use $_SESSION['username'] in Insert, not $username because it isn't a global variable.

Nike555 Posts: 12
I tryed, nothing.
Nothing in php_error.log, in error.log, in mysql_error.log.
Maybe problem is this error:
prntscr.com/9bv7ki

Admin Posts: 805
I not know what the problem is.
Try download the script again and use it. I just tested it and it works, as you can see in this screenshoot:
prntscr.com/9c2977

And, these are the settings I made in votimg.php (using all the files from voting_up_down_set_time.zip)

Code: Select all

define('NEXTV', strtotime('+12 hours'));  // LINE 17, TO CAN VOTE EVERY 12 HOURS

// If you want than only the logged users to can vote the element(s) on page, sets USRVOTE to 0
// And sets $_SESSION['username'] with the session that your script uses to keep logged users
define('USRVOTE', 0);
if(USRVOTE !== 1) {
  if(!isset($_SESSION)) session_start();
  $_SESSION['username'] ='Other_User';  //HERE I ADD THE USERNAME
  if(isset($_SESSION['username'])) define('VOTER', $_SESSION['username']);
}   
- If for you not works, maybe the problem is in the other script (with the login), or from other javascript code in your page.

Nike555 Posts: 12
Big thanks
I added in class.voting.php

Code: Select all

if(defined('USRVOTE') && USRVOTE === 0) { if(defined('VOTER')){ $this->voterUser = VOTER; $this->voter = $_SERVER['REMOTE_ADDR'];}}  
In function setVotDb() i commented this row (if need keep votes when you vote again with this account):

Code: Select all

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

Code: Select all

$this->sqlExecute("INSERT INTO `$this->votusers` (`username`, `account`, `nextv`, `voter`, `item`) VALUES ('$this->voterUser', '$account', ". NEXTV .", '$this->voter', '".$items[0]."')");

with:

Code: Select all

//check if the users can vote, register the vote
$this->sqlExecute("SELECT * FROM `$this->votusers` WHERE `nextv`>$this->time AND `username`='$this->voterUser'");
  if($this->affected_rows == 0) {
    $this->sqlExecute("INSERT INTO `$this->votusers` (`username`, `account`, `nextv`, `voter`, `item`) VALUES ('$this->voterUser', '$account', ". NEXTV .", '$this->voter', '".$items[0]."')");
    }
}  
And with this script you will check login if this login can vote (expired time from column `nextv`).
And Works fine.
Thanks Admin for you help.