你好我想用cookie保存我的颜色来自输入,如果用户刷新页面颜色将被保存并准备好下次使用.Cookies必须只设置1小时,如果过期的cookie将被删除。请继续我的英语和感谢所有帮助
<table>
<form action='' method='post'>
<tr><td><label>URL adresa: </label></td><td><input type='text' placeholder='napr.google.com' name='url' /></td></tr>
<tr><td><label>Titulek: </label></td><td><input type='text' placeholder='napr.google' name='title' /></td></tr>
<tr><td><label>Vyberte barvu: </label></td><td><input type='color' name='color' /></td></tr>
<tr><td><label>Otevřít v novém okně </label></td><td><input type='checkbox' name='window' />
<tr><td></td><td><input type='submit' name='submit' /></td></tr>
</form>
<?php
if (isset($_POST['submit']))
{
if (!empty($_POST['url']))
{
if (!empty($_POST['title']))
{
$url = $_POST['url'];
$title = $_POST['title'];
$color = $_POST['color'];
$explode1 = explode("/",$url);
$explode2 = explode(".",$explode1[2]);
setcookie("color", $color, time() + 3600, '/');
if (isset($_POST['window']))
{
setcookie("window", $_POST['window'], time()+3600, '/');
echo "<a style='color:" . $_COOKIE['color'] . "' href=" . $url . " target='_blank'>" . $explode2[1] . "." . $explode2[2] . "</a>";
} elseif (isset($_COOKIE['window'])) {
echo "<a style='color:" . $_COOKIE['color'] . "' href=" . $url . " target='_blank'>" . $explode2[1] . "." . $explode2[2] . "</a>";
} else {
echo "<a style='color:" . $_COOKIE['color'] . "' href=" . $url . ">" . $explode2[1] . "." . $explode2[2] . "</a>";
}
}
}
}
&GT;
答案 0 :(得分:0)
您永远不会将$color
保存到您的Cookie中:
if (isset($_POST['window'])) {
setcookie("window", $color, time() + 3600, '/');
echo "<a style='color:" . $color . "' href=" . $url . " target='_blank'>" . $explode2[1] . "." . $explode2[2] . "</a>";
}
或者如果$ color需要单独保存:
setcookie("color", $color, time() + 3600, '/');
就个人而言,我会将它保存到会话中,如此:
session_start();
$_SESSION['color'] = $color;
希望有所帮助。 编辑:
if (isset($_POST['submit']) && (!empty($_POST['url']) && (!empty($_POST['title'])) {
$url = $_POST['url'];
$title = $_POST['title'];
$color = (($_POST['color'] == $_COOKIE['color'])?$_COOKIE['color']:$_POST['color']);
$explode1 = explode("/",$url);
$explode2 = explode(".",$explode1[2]);
setcookie("color", $color, time() + 3600, '/');
if (isset($_POST['window'])) {
setcookie("window", $_POST['window'], time()+3600, '/');
echo "<a style='color: $color' href='$url' target='_blank'>$explode2[1]}.{$explode2[2]}</a>";
} else {
echo "<a style='color: $color' href='$url'>{$explode2[1]}.{$explode2[2]}</a>";
}
}