Hola a todos!
A ver si alguien me puede echar una mano.
Necesito en un sistema de votaciones de imagenes restringir el voto por imagen, ip y dia, y no se como y donde incorporarlo. El código es el siguiente:
Código PHP:
<?php
//load configuration
require("config.php");
//connect to database
@mysql_connect($db_server,$db_user,$db_password) or die("No pudo conectarse con la base de datos del servidor. Compruebe las variables \$db_server, \$db_user y \$db_password en config.php");
@mysql_select_db($db_name) or die("La selección de la base de datos ha fallado. Compruebe la variable \$db_name en config.php");
//update votes
$id = $_POST["id"];
$vote = $_POST["vote"];
if(isset($id) && isset($vote)){
@mysql_query("UPDATE $db_table_pictures SET points=points+$vote,votes=votes+1 WHERE id=$id;");
}
//store comment
$comment_name = $_POST["comment_name"];
$comment_text = $_POST["comment_text"];
if(isset($comment_name) && isset($comment_text) && strlen($comment_name)>2 && strlen($comment_text)>2){
$comment_name = str_replace("<","<",$comment_name);
$comment_name = str_replace(">",">",$comment_name);
$comment_name = stripslashes($comment_name);
$comment_text = str_replace("<","<",$comment_text);
$comment_text = str_replace(">",">",$comment_text);
$comment_text = stripslashes($comment_text);
@mysql_query("INSERT INTO $db_table_comments (id_pic,name,text,date) VALUES({$_POST["show"]},'$comment_name','$comment_text',".time().");");
}
//image to be displayed
if(!isset($_GET["show"]) && !isset($_POST["show"])){
//random image
$ids = array();
$result = @mysql_query("SELECT id FROM $db_table_pictures;");
list($min) = @mysql_fetch_row(@mysql_query("SELECT id FROM $db_table_pictures WHERE id>1 ORDER BY id ASC;"));
list($max) = @mysql_fetch_row(@mysql_query("SELECT id FROM $db_table_pictures WHERE id>1 ORDER BY id DESC;"));
while(list($ids[]) = @mysql_fetch_row($result));
while(true){
srand((double)microtime()*1000000);
$show = rand($min,$max);
if(array_search($show,$ids)){
break;
}
}
}else{
//specific image
$show = isset($_GET["show"]) ? $_GET["show"] : $_POST["show"];
}
$row = @mysql_fetch_array(@mysql_query("SELECT * FROM $db_table_pictures WHERE id=$show;"));
list($next) = @mysql_fetch_row(@mysql_query("SELECT id FROM $db_table_pictures WHERE id>$show ORDER BY id ASC LIMIT 0,1;"));
//calculate points
if($row["points"] != 0){
$row["points"] = number_format($row["points"]/$row["votes"],2,".","");
}
//next image
if(!$next){
list($next) = @mysql_fetch_row(@mysql_query("SELECT id FROM $db_table_pictures WHERE id>1 ORDER BY id ASC LIMIT 0,1;"));
}
//print header
echo $header;
//print html
?>
</td>
</tr>
<tr>
<td colspan="2" height="8"> </td>
</tr>
<tr>
<td align="center" colspan="2" height="<?php echo $img_height; ?>"><img alt="" border="0" src="<?php echo $img.$show.$img_format_info; ?>"></td>
</tr>
<tr>
<td colspan="2" height="10"> </td>
</tr>
<tr>
<td colspan="2" height="10">
<form action="vota.php" method="post">
<table align="center" border="0" cellpadding="2" cellspacing="0">
<tr>
<td rowspan="2"><img src="imagenes/bad.png" width="50" height="50" /></td>
<td rowspan="2" width="10"> </td><?php
//voting form
for($i=1;$i<=$max_points;$i++){
?><td align="center"><?php echo $i; ?></td><?php
}
?><td rowspan="2" width="10"> </td>
<td rowspan="2"><img src="imagenes/good.png" width="50" height="50" /></td>
</tr>
<tr><?php
for($i=1;$i<=$max_points;$i++){
?><td align="center">
<input type="radio" name="vote" value="<?php echo $i; ?>" onclick="submit();"></td><?php
}
?></tr>
</table>
<input name="show" type="hidden" value="<?php echo $next; ?>">
<input name="id" type="hidden" value=" <?php echo $row["id"]; ?>">
</form>
</td>
</tr>
<tr>
<td colspan="2" height="10"> </td>
</tr>
<tr>
<td valign="top" width="50%">Imagen de <span class="resaltar"><?php echo $row["creator"]; ?> <?php echo $row["apellidos"]; ?></span><br><br>
<span class="resaltar">Descripción de la Imágen:</span><br /><br /><?php echo $row["comment"]; ?><br><br></td>
<td align="right" valign="top" width="50%">Puntos: <span class="resaltar"><?php echo $row["points"]; ?></span> (<?php echo $row["votes"]; ?> votos)<br><br></td>
</tr>
<td colspan="2">
<hr><?php
$result = @mysql_query("SELECT * FROM $db_table_comments WHERE id_pic={$row["id"]} ORDER BY date DESC;");
if(@mysql_num_rows($result) == 0){
?>Sin comentarios<?php
}else{
while($row = @mysql_fetch_array($result)){
?><span class="resaltar"><?php echo $row["name"]; ?></span> Escribe el <?php echo date("m/d/Y",$row["date"]); ?> a las <?php echo date("h:i a",$row["date"]); ?>:<br>
<?php echo $row["text"]; ?>
<br><br><?php
}
}
?><hr>
<b class="resaltar">Escribe un comentario:</b>
<form action="vota.php" method="post">
<table align="center" border="0" cellpadding="5" cellspacing="0">
<tr>
<td>Su nombre:</td>
<td><input maxlength="50" name="comment_name" size="30" type="text"></td>
</tr>
<tr>
<td valign="top">Comentario:</td>
<td><textarea cols="70" name="comment_text" onkeyup="this.value=this.value.slice(0,<?php echo $comment_size; ?>);" rows="6"></textarea></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Enviar"></td>
</tr>
</table>
<input name="show" type="hidden" value="<?php echo $show; ?>">
</form><?php
//print footer
echo $footer;
//close database connection
@mysql_close();
?>
y he creado la siguiente tabla para almacenar los datos de la ip, la fecha, y la votacion de cada imagen:
Código PHP:
CREATE TABLE IF NOT EXISTS `ip_votos` (
`id` int(3) NOT NULL auto_increment,
`ip` varchar(16) character set utf8 NOT NULL,
`id_imagen` int(4) NOT NULL,
`date` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
a ver si me podeis dar la solucion o si conoceis otra forma mejor de hacerlo.
Gracias de antemano!!