简体   繁体   中英

Won't set cookie on localhost?

Hello im trying to create a cookie on localhost

This is my code:

<?php
      SESSION_START();
      if (isset($_GET['lang'])) {
        $available_langs = array("se", "uk");
        if (in_array($_GET['lang'], $available_langs)) {
          setcookie("wb_lang", $_GET['lang'], "1", "/", "localhost");
        }
        else {

          $_SESSION['sess_error'] = LANGUAGE_YOU_SELECTED_DOESNT_EXIST;
        }
        header("Location: /");
        exit;
      }
?>

That part set the cookie, if i take die after the cookie is set i get the an answer from the cookie.

But when i try to use the cookie on index.php, it doesn't exist at all..

The language.php is in a subfolder named /modules/language.php it that is any problem?

So the cookie is set, but only in language.php and i can't seem to find any answer for this either, i tried several answers without any success.

You've set your $expire (third) argument to "1" , which is always in the past, so the cookie will always instantly expire.

If this is a session cookie, you can use 0 .

If you want it to persist when the browser closes and reopens, you will need to set a value in the future, eg time() + 365*24*60*60 for the cookie to survive approximately 1 year.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM