Hi can someone give me php code for voting once a day .
9 replies to this topic
#1
Posted 24 June 2011 - 04:37 AM
|
|
|
#2
Posted 24 June 2011 - 01:54 PM
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...
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
I study Information Systems at Karlstad University when I'm not on CodeCall
#3
Posted 27 June 2011 - 04:11 AM
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
Posted 27 June 2011 - 09:55 AM
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:
and btw, unless there's only 1 item to vote for, I'd expect a field like 'item_id' inside of the table 'votes'.
<?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
Posted 27 June 2011 - 12:11 PM
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
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
<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
Posted 28 June 2011 - 01:28 AM
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
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 )
<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
Posted 28 June 2011 - 12:25 PM
I put 10 to test and this code dont work !!!!!!! After 10 sec i can vote many times
#8
Posted 29 June 2011 - 03:42 AM
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:
and this is the code:
Works perfect on my home PC, tested several times.
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
Posted 01 July 2011 - 01:30 PM
10x man it works :thumbup:
#10
Posted 05 October 2011 - 09:07 AM
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
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


Sign In
Create Account


Back to top









