Jump to content

Vote once a day problem

- - - - -

  • Please log in to reply
9 replies to this topic

#1
portosbg

portosbg

    Newbie

  • Members
  • PipPip
  • 12 posts
Hi can someone give me php code for voting once a day .

#2
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#
no, but a hint of how to do it.

register votes in a table. next time the user votes, check the timestamp of that vote and check if it's more or less than 24 hours past...
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#3
portosbg

portosbg

    Newbie

  • Members
  • PipPip
  • 12 posts
if($_POST['submit']=='give point')

{

    $ip = $_SERVER["REMOTE_ADDR"];

    $time = time();

    $vtime = "60*60*24";

    $sql = mysql_query("SELECT time FROM ips WHERE ip = '$ip'") or die (mysql_error());

    if($row = mysql_fetch_array($sql)) {

        $calc = $row['time'] + $vtime;

            if ($calc > $time) {

              echo "onli one time ";}

            else{$query = mysql_query(" UPDATE `members` SET point = point + 1 WHERE id = $id;") or die(mysql_error());}

     }

    else {

            $sql = mysql_query("INSERT INTO votes (ip, time) VALUES ('$ip','$time')") or die (mysql_error());

           

          }

I made this one but it not work . I have 2 data bases first one -members there are records of id point and pass
and ips where are ip of voters and time .Please tell my what is wrong

#4
webcodez

webcodez

    Programmer

  • Members
  • PipPipPipPip
  • 149 posts
First thing I noticed about your code is that $vtime is written as a string, which is probably not desired. Also you assume that the user has only made one vote ( as mysq_fetch_assoc can only store 1 row/entry found per time ) and did not order the query by time or anything at all. Assuming that you've got a table votes containing all votes ( by IP and Time ), I'd write the code like this:


<?php


$timeout = 60*60*24; //1 day timeout

$time     = time(); //current time

$out      = $time-$timeout; //all votes of the current user that were done later than this count


$ip = $_SERVER['REMOTE_ADDR']; //user ip


$check_double = mysql_query("SELECT * FROM votes WHERE ip = '$ip' AND time > $out "); //check user's votes from last 24 hours ($timeout period)


if(mysql_num_rows($check_double) > 0) { //already voted in the last 24 hours? ($timeout period)

   echo "Sorry, you may only vote ONCE a day.";

}else{

   $vote = mysql_query("INSERT INTO votes(ip, time)VALUES('$ip', '$time')");

}


?>


and btw, unless there's only 1 item to vote for, I'd expect a field like 'item_id' inside of the table 'votes'.

#5
portosbg

portosbg

    Newbie

  • Members
  • PipPip
  • 12 posts
pfffffffffff i forgot <form action="" method="post">
<input type="submit" name="submit" value="give point" class="bt_register" />
</form>
there is a form for voting- this is a real problem :( sorry .
.I wanna make voting system for each user not for one thing That why i have 2 DB Can you tell mi how when i click the submit button to give point to user and then if i vote again of this ip before 24 hours there echo error msg


<form action="" method="post">

<input type="submit" name="submit" value="dai to4ka" class="bt_register" />

</form>

<?php

$id = (int)$_GET['id'];

if($_POST['submit']=='dai to4ka')

{

$timeout=10;

$time=time();

$out=$time-$timeout;

$ip=$_SERVER['REMOTE_ADDR'];

$check_double=mysql_query("SELECT * FROM ips WHERE ip='$ip' AND time>$out");


if(mysql_num_rows($check_double)>0){

echo "one time pls ";

}

else{

$vote=mysql_query("INSERT INTO ips(ip,time) VALUES('$ip','$time')");

$query = mysql_query(" UPDATE `tz_members` SET point = point + 1 WHERE id = $id;") or die(mysql_error());

}


}

i made this but it isnt working :confused: when the time expires i can give a lot points before that it
works -echo one time pls ,but when make a row in DB it echo one time pls and add 1 point :cursing:
Sorry for my bad english

Edited by portosbg, 27 June 2011 - 01:11 PM.


#6
webcodez

webcodez

    Programmer

  • Members
  • PipPipPipPip
  • 149 posts
Ehm you set the timeout to 10, which means a user will be able to revote in 10 seconds. Make the timeout like 1 day as you desire -> $timeout = 60*60*24; Because timeout is in seconds



<form action="" method="post">

<input type="submit" name="submit" value="dai to4ka" class="bt_register" />

</form>

<?php

$id = (int)$_GET['id'];

if($_POST['submit']=='dai to4ka')

{

$timeout=60*60*24; //60*60*24 seconds = 1 day

$time=time();

$out=$time-$timeout;

$ip=$_SERVER['REMOTE_ADDR'];

$check_double=mysql_query("SELECT * FROM ips WHERE ip='$ip' AND time>$out")or die(mysql_error());


if(mysql_num_rows($check_double)>0){

echo "one time pls ";

}

else{

$vote=mysql_query("INSERT INTO ips(ip,time) VALUES('$ip','$time')");

$query = mysql_query(" UPDATE `tz_members` SET point = point + 1 WHERE id = $id;") or die(mysql_error());

}


}

?>


Also let me know if there's any mysql error ( I put "or die(mysql_error())" behind the query so that it will output a mysql error if any occured )

#7
portosbg

portosbg

    Newbie

  • Members
  • PipPip
  • 12 posts
I put 10 to test and this code dont work !!!!!!! After 10 sec i can vote many times

#8
webcodez

webcodez

    Programmer

  • Members
  • PipPipPipPip
  • 149 posts
Hmm it should, I cannot test it for you right now ( have to go work ) but when I get back from work I'll create the database and test it on my pc. No panic :rolleyes:

EDIT: I tried it at home and it works like a charm for me, so there's got to be either something wrong with your Apache/MySql Server or database. This is the database table 'ips' I created to test it with:


CREATE TABLE IF NOT EXISTS `ips` (

  `id` int(250) NOT NULL AUTO_INCREMENT,

  `ip` varchar(30) NOT NULL,

  `time` int(250) NOT NULL,

  PRIMARY KEY (`id`)

) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;


and this is the code:


<?php

mysql_connect("localhost", "root", "password");

mysql_select_db("webcodez");

?>

<form action="<?=$_SERVER['PHP_SELF'];?>" method="post"> 

<input type="submit" name="submit" value="dai to4ka" class="bt_register" /> 

</form> 

<?php 

$id = (int)$_GET['id']; 

if($_POST['submit']=='dai to4ka') 

{ 

$timeout=10; //60*60*24 seconds = 1 day 

$time=time(); 

$out=$time-$timeout; 

$ip=$_SERVER['REMOTE_ADDR']; 

$check_double=mysql_query("SELECT * FROM ips WHERE ip='$ip' AND time>$out")or die(mysql_error()); 


if(mysql_num_rows($check_double)>0){ 

echo "one time pls "; 

} 

else{ 

echo "success";

$vote=mysql_query("INSERT INTO ips(ip,time) VALUES('$ip','$time')"); 

//$query = mysql_query(" UPDATE `tz_members` SET point = point + 1 WHERE id = $id;") or die(mysql_error()); 

} 


} 

?>


Works perfect on my home PC, tested several times.

Edited by webcodez, 30 June 2011 - 03:21 AM.


#9
portosbg

portosbg

    Newbie

  • Members
  • PipPip
  • 12 posts
10x man it works :thumbup:

#10
v3inte

v3inte

    Newbie

  • Members
  • Pip
  • 4 posts
hello there..
im using this code too..

i want to put some twist to it.. but i dont know how to do..

i want to have the Status first from the admin..
if the status is 1 = On, then the user will able to vote..
and if the status is 2 = Off, then the error message will appear to the user saying Voting is Off..

Thank you so much




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users