Jump to content
Fórum Script Brasil
  • 0

(Resolvido) Aplicar cookie nas variáveis


Brandow

Question

Olá eu tentei aplicar um cookie no meu sistema de votação mais ele não funciona olha o codigo.

<?php
// If the user hasn't said they are sure they want to vote.
if (!isset($_POST['submitted'])) {

    // Check if the ID was submitted, AND if it is numeric (a number)
    if (isset($_GET['id']) && is_numeric($_GET['id'])) {
    
        // Connect to our database
        require_once('mysql_connect.php');
        
        $id = mysql_real_escape_string($_GET['id']);
        
        $query = "SELECT id, name FROM dados_usuarios WHERE id = $id";
        $result = mysql_query($query) OR die (mysql_error());
        $row = mysql_fetch_array($result, MYSQL_ASSOC);
?>
        <strong>Você quer votar no <?php echo $row['name']; ?> clique no botão abaixo</strong><br />
        
        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
        <input type="submit" name="vote" value="Vote For <?php echo $row['name']; ?>" />
        <input type="hidden" name="submitted" value="TRUE" />
        <input type="hidden" name="id" value="<?php echo $row['id']; ?>" />
        </form>
<?php
    } else {
        echo '<font color="red">You need to select a site to vote for!</font>';
    }
} else {
    // Connect to our database
    require_once('mysql_connect.php');
        
    $id = mysql_real_escape_string($_POST['id']);
    
    // Select the site we want to add 1 vote to
    $query = "SELECT id, votes FROM dados_usuarios WHERE id = $id";
    $result = mysql_query($query) OR die(mysql_error());
    $row = mysql_fetch_array($result, MYSQL_ASSOC);
    
    $votes = $row['votes'];
    $id = $row['id'];
    
    $cookie_name = 'vote_'.$id;
    
    if (!isset($_COOKIE['vote_'.$id])) {
        // Add 1 to the previous hit amount
        $votes++;
        
        // Update our websites hit amount
        $query = "UPDATE dados_usuarios SET votes = $votes WHERE id = $id";
        $result = mysql_query($query) OR die(mysql_error());
        
        setcookie("cookie_name", $_SERVER['REMOTE_ADDR'], time()+86400); 
        
                echo 'você já voltou! <a href="/topsite/">Click here to go back to our homepage.</a>';
    } else {
        echo 'vocÊ já votou.';
    }
}


?>

Link to comment
Share on other sites

5 answers to this question

Recommended Posts

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Forum Statistics

    • Total Topics
      152.2k
    • Total Posts
      652k
×
×
  • Create New...