php setcookie没有设置cookie

时间:2014-07-13 16:03:54

标签: php ajax cookies

我的setcookie功能没有按预期设置cookie。它曾经工作,我改变了一些东西,现在它不会设置cookie,我无法弄清楚原因。

简而言之,这是一个评论评级脚本,它通过另一个页面(index.php)的ajax进行处理。用户单击链接,ajax将该模式,评级,comment_id发送到此页面。一切正常,我已经独立地回应了变量来测试它。

唯一不起作用的是setcookie - 我使用chrome并且无法看到我的localhost MAMP环境中设置的cookie。

rating.php:

    <?php
// simple comment up and down voting script. connection file does not have any output, only database password info.
require_once('../connections/connection_local.php'); 


$mode = $_GET['mode'];
$rating = (int) $_GET['rating'];
$comment_id = (int) $_GET['id'];
$cookie = "dlc_" . $mode . "_" . $comment_id;
$expiry = 1209600 + time();  // 14 day expiry
$value = time();
// I tested the above and each variable echoes out correctly. The $cookie echoes correctly as "dlc_style_4" or "dlc_mode_4" depending on what the previous page sends over.

if ($mode=="style")
    {
    if(!isset($_COOKIE[$cookie])) 
        {
        setcookie ($cookie, $value , $expiry, "/");
        // update the rating in comments    
        $ratingUpdate_query = "UPDATE comments SET stylerating = stylerating + {$rating} WHERE id={$comment_id}";
        mysqli_query ($db_connection, $ratingUpdate_query) or die(mysqli_error($db_connection));

        // record the score in scorechart
        $event = logged_in()? $_SESSION["user_id"]. ':' . $cookie : $_SERVER['REMOTE_ADDR']. ':' . $cookie;
        $scorechart_query= "..... query not included, but it works fine and updates the database table correctly ... ";
        mysqli_query ($db_connection, $scorechart_query) or die(mysqli_error($db_connection));
        }
    elseif (isset($_COOKIE[$cookie])) {
        echo "<script> console.log('Cookie already set for this id');</script>";
    }

    else {echo "<script> console.log('Please turn on your cookies');</script>";}
    }

elseif ($mode=="content")
    {
    if(!isset($_COOKIE[$cookie])) 
        {
        // update the rating in comments    
        setcookie ($cookie, $value  , $expiry, "/");
        $ratingUpdate_query = "UPDATE comments SET contentrating = contentrating + {$rating}  WHERE id={$comment_id}";
        mysqli_query ($db_connection, $ratingUpdate_query) or die(mysqli_error($db_connection));

        // record the score in scorechart
        $event = logged_in()? $_SESSION["user_id"]. ':' . $cookie : $_SERVER['REMOTE_ADDR']. ':' . $cookie;
        $scorechart_query = "..... query not included here, but it works fine and updates the database table correctly ... ";
        mysqli_query ($db_connection, $scorechart_query) or die(mysqli_error($db_connection));
        }
    elseif (isset($_COOKIE[$cookie])) {
        echo "<script> console.log('Cookie already set for this id');</script>";
    }
    else {echo "<script> console.log('Please turn on your cookies');</script>";}    
    }

else {echo "<script> console.log('Failed to update rating. Please turn on your cookies');</script>";}
// check if the comment was upvoted by somebody who has a comment on the other side. if so, +1 to persuasiveness

?>

0 个答案:

没有答案