【问题标题】:PHP Cookie not working on redirectPHP Cookie 不适用于重定向
【发布时间】:2016-03-08 15:11:26
【问题描述】:

我有相当沮丧的问题。我正在尝试将表单变量存储到 cookie 中。

<form id = "vraagStellen" action = "shoutbox.php" method = "post" class = "col-lg-4 col-md-6 col-sm-12" role="form">
    <a name = "bottomOfPage"></a>
    <div class = "selectAfbeelding form-group">
        <label for "gamertag">Gamertag:</label>
        <input type = "text" id = "gamertag" name = "gamertag" class = "form-control" maxlength="30" value="<?php if(isset($_COOKIE['gamertag'])){echo $_COOKIE['gamertag'];} else {echo "";} ?>" placeholder="<?php if(isset($_COOKIE['gamertag'])){echo $_COOKIE['gamertag'];} else {echo "Hier typen";} ?>" required>
    </div>
    <div class = "selectVraag form-group">
        <label for "bericht">Bericht:</label>
        <textarea id = "bericht" onkeyup = "count()" name = "bericht" class = "form-control" maxlength="100" cols = "40" rows = "2" placeholder = "Hier typen" required></textarea>
        <div id = "countDiv"></div>
    </div>
    <div class = "form-group">
        <input type="submit" name = "verzend" value = "Verzenden" id = "verzenden">
        <input type="reset" name = "reset" value = "Leegmaken" id = "reset"> 
    </div>
</form> 

还有 php:

if (isset($_POST['gamertag']) && isset($_POST['bericht']) && !empty($_POST['gamertag']) && !empty($_POST['bericht'])){

$afbeelding_before = $_POST['gamertag'];
$vraag_before = $_POST['bericht'];

$cookie_var = $_POST['gamertag'];

$afbeelding = mysql_real_escape_string($afbeelding_before);
$vraag = mysql_real_escape_string($vraag_before);

setcookie("gamertag", $cookie_var, time() + (86400 * 30), "/",$_SERVER['SERVER_NAME']);

//echo $afbeelding . $vraag;

$sql3="INSERT INTO `shoutbox` (`id`, `gamertag`, `bericht`, `date`) VALUES (NULL, '$afbeelding', '$vraag', CURRENT_TIMESTAMP)";
mysql_query($sql3,$con);

echo "post gamertag ".$_POST['gamertag']."<BR>";
echo "cookie ". $_COOKIE['gamertag']."<BR>";

header('Location: http://link.nl#shoutbox');

}

else{
    echo 'Er ging iets fout...';
}

从 PHP 代码重定向时,cookie 未显示在名为“gamertag”的表单输入中。但是当我从链接中删除#shoutbox 时,它会正确显示cookie。浏览不同的页面不是问题。仅当从 PHP 代码重定向时,它才不会显示。

希望有人能帮帮我!

谢谢!

【问题讨论】:

    标签: php forms cookies


    【解决方案1】:

    确保 $_SERVER['SERVER_NAME'] 中的值恰好是 link.nl 而不是 www.link.nl。对于浏览器,这是两个不同的值,因此它们会创建两个不同的 cookie。

    【讨论】:

      【解决方案2】:

      也许您使用了无效的会话 ID 或 PHPSESSID。你能查一下PHPSESSID吗?

      【讨论】:

        猜你喜欢
        • 2012-03-27
        • 1970-01-01
        • 2011-04-14
        • 1970-01-01
        • 1970-01-01
        • 2014-03-27
        • 1970-01-01
        • 1970-01-01
        • 2012-10-10
        相关资源
        最近更新 更多