PHP setcookie warning

Posted by Ranking on Stack Overflow See other posts from Stack Overflow or by Ranking
Published on 2011-03-20T00:02:29Z Indexed on 2011/03/20 0:10 UTC
Read the original article Hit count: 123

Filed under:
|

Hello guys, I have a problem with 'setcookie' in PHP and I can't solve it.

so I receive this error "Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\VertrigoServ\www\vote.php:14) in C:\Program Files\VertrigoServ\www\vote.php on line 86"

and here is the file.. line 86 is setcookie ($cookie_name, 1, time()+86400, '/', '', 0); is there any other way to do this ??

<html>
<head>
<title>Ranking</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body bgcolor="#EEF0FF">
<div align="center"> 
<br/> 
<div align="center"><div id="header"></div></div>
<br/> 
<table width="800" border="0" align="center" cellpadding="5" cellspacing="0" class="mid-table"> 
    <tr><td height="5">
        <center> 
            <table border="0" cellpadding="0" cellspacing="0" align="center" style="padding-top:5px;"> 
                <tr> 
                    <td align="center" valign="top"><img src="images/ads/top_banner.png"></td> 
                </tr> 
            </table> 
        </center> 
    </td></tr> 
    <tr><td height="5"></td></tr> 
</table> 
<br/> 
<?php
    include "conf.php";

    $id = $_GET['id'];
    if (!isset($_POST['submitted']))
    {
        if (isset($_GET['id']) && is_numeric($_GET['id']))
        {
            $id = mysql_real_escape_string($_GET['id']);
            $query = mysql_query("SELECT SQL_CACHE id, name FROM s_servers WHERE id = $id");
            $row = mysql_fetch_assoc($query);
            ?>
            <form action="" method="POST">
                <table width="800" height="106" border="0" align="center" cellpadding="3" cellspacing="0" class="mid-table"> 
                    <tr><td><div align="center">
                        <p>Code: <input type="text" name="kod" class="port" /><img src="img.php" id="captcha2" alt="" /><a href="javascript:void(0);" onclick="document.getElementById('captcha2').src = document.getElementById('captcha2').src + '?' + (new Date()).getMilliseconds()">Refresh</a></p><br />
                        <p><input type="submit" class="vote-button" name="vote" value="Vote for <?php echo $row['name']; ?>" /></p>
                        <input type="hidden" name="submitted" value="TRUE" />
                        <input type="hidden" name="id" value="<?php echo $row['id']; ?>" />
                    </div></td></tr> 
                    <tr><td align="center" valign="top"><img src="images/ads/top_banner.png"></td></tr> 
                </table> 
            </form> 
            <?php
        }
        else
        {
            echo '<font color="red">You must select a valid server to vote for it!</font>';
        }
    }
    else
    {
        $kod=$_POST['kod'];
        if($kod!=$_COOKIE[imgcodepage])
        {
            echo "The code does not match";
        }
        else
        {
            $id = mysql_real_escape_string($_POST['id']);
            $query = "SELECT SQL_CACHE id, votes FROM s_servers 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;
            $ip = $_SERVER['REMOTE_ADDR'];
            $ltime = mysql_fetch_assoc(mysql_query("SELECT SQL_CACHE `time` FROM `s_votes` WHERE `sid`='$id' AND `ip`='$ip'"));
            $ltime = $ltime['time'] + 86400;
            $time = time();

            if (isset($_COOKIE['vote_'.$id]) OR $ltime > $time)
            {
                echo 'You have already voted in last 24 hours! Your vote is not recorded.';
            }
            else
            {
                $votes++;
                $query = "UPDATE s_servers SET votes = $votes WHERE id = $id";
                $time = time();
                $query2 = mysql_query("INSERT INTO `s_votes` (`ip`, `time`, `sid`) VALUES ('$ip', '$time', '$id')");
                $result = mysql_query($query) OR die(mysql_error());
                setcookie ($cookie_name, 1, time()+86400, '/', '', 0);
            }
        }
    }
?>
<p><a href="index.php">[Click here if you don't want to vote]</a></p><br/>
<p><a href="index.php">Ranking.net</a> &copy; 2010-2011<br> </p> 
</div> 
</body> 
</html> 

Thanks a lot!

© Stack Overflow or respective owner

Related posts about php

Related posts about setcookie