setcookie并询问投票系统的cookie值

时间:2015-04-30 00:05:42

标签: php cookies vote

我想设置一个cookie,让用户只在4个类别中投票一次(悲伤,激怒,有趣,酷)。 我是一个初学者,所以我知道我的代码非常不方便,但这是我的第一次尝试,所以如果我或多或少有几行,它对我来说很重要。 由于某种原因,浏览器不会加载vote.php文件(当用户单击一个投票选项时调用)。我无法找到错误......也许有人立即看到了它? [在我插入cookie之前,代码文件完全加载,所以调用不是问题。]

<?php
require_once('connect.php');
$newsid = $_POST['id'];

if (isset($_POST['submit'])) {
    if(isset($_COOKIE['votecookie']) && $_COOKIE['votecookie'] !== 'sad') {
        if($_COOKIE['votecookie'] == 'cool') {  
            $query = mysql_query('UPDATE `index` SET cool=cool-1 WHERE id = {$newsid}');
        }
        if($_COOKIE['votecookie'] == 'funny') { 
            $query = mysql_query('UPDATE `index` SET funny=funny-1 WHERE id = {$newsid}');
        }
        if($_COOKIE['votecookie'] == 'sad') {   
            $query = mysql_query('UPDATE `index` SET enraging=enraging-1 WHERE id = {$newsid}');
        }
        setcookie('votecookie', 'sad');
        $query = mysql_query('UPDATE `index` SET sad=sad+1 WHERE id = '{$newsid}'');
        $hosts = $_SERVER['HTTP_HOST'];
        $uris = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
        $extras = 'news.php?id={$newsid}&sad=1';
        header('Location: http://$hosts$uris/$extras');
        exit;
    }
    if(!isset($_COOKIE['votecookie']) {
            setcookie('votecookie', 'sad');
            $query = mysql_query('UPDATE `index` SET sad=sad+1 WHERE id = '{$newsid}'');
            $hosts = $_SERVER['HTTP_HOST'];
            $uris = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
            $extras = 'news.php?id={$newsid}&sad=1';
            header('Location: http://$hosts$uris/$extras');
            exit;
        }

        if(isset($_COOKIE['votecookie']) && $_COOKIE['votecookie'] == 'sad') {
            $hosts= $_SERVER['HTTP_HOST'];
            $uris = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
            $extras = 'news.php?id={$newsid}&sad=0';
            header('Location: http://$hosts$uris/$extras');
            exit;
        }
        }
    else {
        $hosts= $_SERVER['HTTP_HOST'];
        $uris = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
        $extras = 'news.php?id={$newsid}&sad=0';
        header('Location: http://$hosts$uris/$extras');
        exit;
        ?>

1 个答案:

答案 0 :(得分:1)

如果有人对答案感兴趣:这就是我现在的做法(我的代码中出现了一些错误,推理错误)。

<?php
require_once('connect.php');
$newsid = $_POST['id'];
if (isset($_POST['submit'])) {
    if(isset($_COOKIE[$newsid]) && $_COOKIE[$newsid] != 'sad') {
        if($_COOKIE[$newsid] == 'cool') {   
        $query = mysql_query("UPDATE `index` SET cool=cool-1 WHERE id = '{$newsid}'");
        }
        if($_COOKIE[$newsid] == 'funny') {  
        $query = mysql_query("UPDATE `index` SET funny=funny-1 WHERE id = '{$newsid}'");
        }
        if($_COOKIE[$newsid] == 'enraging') {   
        $query = mysql_query("UPDATE `index` SET enraging=enraging-1 WHERE id = '{$newsid}'");
        }
    $query = mysql_query("UPDATE `index` SET sad=sad+1 WHERE id = '{$newsid}'");
    setcookie($newsid, "sad", time() + (10 * 365 * 24 * 60 * 60));
    $hosts = $_SERVER['HTTP_HOST'];
    $uris = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
    $extras = "news.php?id={$newsid}&sad=1";
    header("Location: http://$hosts$uris/$extras");
        exit;
    }
    if(!isset($_COOKIE[$newsid])) {
        $query = mysql_query("UPDATE `index` SET sad=sad+1 WHERE id = '{$newsid}'");
        setcookie($newsid, "sad", time() + (10 * 365 * 24 * 60 * 60));
        $hosts = $_SERVER['HTTP_HOST'];
        $uris = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
        $extras = "news.php?id={$newsid}&sad=1";
        header("Location: http://$hosts$uris/$extras");
            exit;
    }   
    if(isset($_COOKIE[$newsid]) && $_COOKIE[$newsid] == 'sad') {
        $hosts= $_SERVER['HTTP_HOST'];
        $uris = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
        $extras = "news.php?id={$newsid}&sad=0";
        header("Location: http://$hosts$uris/$extras");
            exit;
    }
}
else {
    $hosts= $_SERVER['HTTP_HOST'];
    $uris = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
    $extras = "news.php?id={$newsid}&sad=0";
    header("Location: http://$hosts$uris/$extras");
    exit;
}
?>