如何制作一个cookie最后一年PHP

I attempted to make a cookie-based theme switcher using PHP, and to make the cookie I use this:

$t = $_GET["t"];
if($t == "dark" || "light" || "mixed") {
    setcookie("theme", $t, time() + 31556926, "/");
} elseif(!isset($t) and !isset($_COOKIE["theme"])) {
    setcookie("theme", "dark", time() + 31556926 , "/");
}
if(isset($_COOKIE["theme"])) {
    echo "<body style=\"background:url(/imgs/" . $_COOKIE['theme'] . ".gif) top left repeat\">";
} else {
    echo "<body style=\"background:url(/imgs/dark.gif) top left repeat\">";
}

But this doesn't seem to work, it reloads the page and sets the cookie, but has a strange property of needing to reload the page again in order for it to change the background, and only lasting for the session. As well as this, when you return to the index.php without ?t=light it stays, but then when you reload it disappears. I couldn't figure out any reason that it should be acting this way.

I think you have syntax errors..Just try it..

$t = $_GET["t"];
if($t == "dark" || $t ==  "light" || $t == "mixed") {
     setcookie("theme", $t, time() + 31556926, "/");
 } elseif(!isset($t) && !isset($_COOKIE["theme"])) {
     setcookie("theme", "dark", time() + 31556926 , "/");
 }
if(isset($_COOKIE["theme"])) {
 echo "<body style=\"background:url(/imgs/" . $_COOKIE['theme'] . ".gif) top  left repeat\">";
} else {
echo "<body style=\"background:url(/imgs/dark.gif) top left repeat\">";
}

Make your life easier using strtotime():

setcookie('theme', 'dark', strtotime('+1 year'), '/');

By the way, cookies should be above everything else - it's part of the header. You should place it above all, else - it may produce errors (if you want to hide the errors you can simply insert @ before setcookie).

Not sure if there is a maximum, however I found a couple of people who suggest it is limited by the unixtime, ie nothing past 2038. However, as others have pointed out, there is no point making it more than 10 years as browsers / computers will expire within 10 years and be reloading or replaced etc. My reference: https://www.quora.com/What-is-the-limit-to-a-persistent-cookies-expiry-date

In the meantime I found this regarding the size and number limits: http://browsercookielimits.squawky.net/

The problem is that you, as you said in comments have the setcookie below other code.

You can not have any output before the setcookie.
Move the code you have in the question as far up as you can.

See here: http://php.net/manual/en/function.setcookie.php

If output exists prior to calling this function, setcookie() will fail and return FALSE.

Edit; and with as far up as possible, I mean above <HTML> tag

Edit2; about the isset.

If(isset($_GET["t"])){
      $t = $_GET["t"];
}Else{
      $t = "dark";
}
if($t == "dark" || "light" || "mixed") {
    setcookie("theme", $t, time() + 31556926, "/");
} elseif(!isset($t) and !isset($_COOKIE["theme"])) {
    setcookie("theme", "dark", time() + 31556926 , "/");
 }
if(isset($_COOKIE["theme"])) {
    echo "<body style=\"background:url(/imgs/" . $_COOKIE['theme'] . ".gif) top left repeat\">";
} else {
     echo "<body style=\"background:url(/imgs/dark.gif) top left repeat\">";
}

It's hard to get it right on phone but you get the idea.