可以 PHP 设置 cookie 功能,覆盖使用 JQuery cookie 创建的 cookie 值

Can PHP set cookie function, Overwrite cookie value created with JQuery cookie

我目前正在尝试设置一个 cookie,当用户登录时,cookie 的值会被 PHP 有效地覆盖,但它只是创建一个单独的 cookie,这是一段代码正在设置 cookie 值

    <?php
include("db_connect.php");

$input_game = $_POST['game'];
$input_user = $_POST['email'];

$sql = "UPDATE users_table SET Pref_Game = '" . $input_game . "' WHERE Email='" . $input_user . "'";
if ($conn->query($sql) === TRUE) {
$cookie_name2 = "content";

$sql="SELECT Pref_Game FROM users_table WHERE Email='$input_user'";
$result = $conn->query($sql);
$row = $result->fetch_object();
setcookie($cookie_name2,$row->Pref_Game, time() + (86400 * 30), "/"); // 86400 = 1 day
} else {
    //Error
}
?>

为了清楚起见,下面是 JQuery cookie 代码:

$("#test-cookie").click(function() {
    $.cookie('content', 'test');
    location.reload();
});

PHP 是否可以 update/overwrite JQuery 创建的 cookie 值?

PHP will overwrite jQuery's cookie if it has the exact same name, path, and domain. What path has the jQuery cookie been written for? Look at it in your browsers development console. If it was within a directory when set, you may need to use an expression like $.cookie('name', 'value', { expires: 7, path: '/' }); to set it for / as the PHP cookie has done.

这回答了我的问题谢谢:

    $("#test-cookie").click(function() {
    $.cookie('content', 'test', { expires: 7, path: '/' });
    location.reload();
});