php - Save color using cookies -
hello want save cookie color input , if user refresh page color saved , ready next use.cookies must set 1 hour , if expired cookies deleted.sorry english , help
<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>"; } } } }
?>
you never save $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>"; }
or if $color needs saved separately:
setcookie("color", $color, time() + 3600, '/');
personally, save session so:
session_start(); $_session['color'] = $color;
hope helps. edit:
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>"; } }