我有相当沮丧的问题。我正在尝试将表单变量存储到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...';
}
Cookie没有显示在名为&#34; gamertag&#34;从PHP代码重定向时。但是当我从链接中删除#shoutbox时,它将正确显示cookie。浏览不同的页面并不是一个问题。只有在从PHP代码重定向时才会显示它。
我希望有人可以帮助我!
谢谢!
答案 0 :(得分:2)
确保$ _SERVER [&#39; SERVER_NAME&#39;]中的值与link.nl完全相同,而不是www.link.nl.对于浏览器,这些是两个不同的值,因此它们创建了两个不同的cookie。
答案 1 :(得分:1)
也许你使用了无效的会话ID或PHPSESSID
。你能查一下PHPSESSID
吗?