簡體   English   中英

PHP Cookie無法在重定向上工作

[英]PHP Cookie not working on redirect

我有相當沮喪的問題。 我正在嘗試將表單變量存儲到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代碼重定向時才顯示。

我希望有一個人可以幫助我!

謝謝!

確保$ _SERVER ['SERVER_NAME']中的值完全是link.nl,而不是www.link.nl。 對於瀏覽器,這是兩個不同的值,因此它們創建兩個不同的cookie。

也許您使用無效的會話ID或PHPSESSID 您可以檢查PHPSESSID嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM