PHP cookie set in second refresh page

前端 未结 5 478
耶瑟儿~
耶瑟儿~ 2020-12-10 08:10

i want to use cookie in web page and i want to use it for save any optional variables. but after use setcookie and refresh page isset()

5条回答
  •  难免孤独
    2020-12-10 08:46

    Cookie is set to a specific path and domain. You should change setcookie() to

    setcookie ("user", "Alex Porter", time()+3600, "/", "youdomain.com");
    

    If set to '/', the cookie will be available within the entire domain(youdomain.com).

    Cookie will be visible after page refresh.

    Your code can be simplifed as well:

     echo "Welcome " . isset($_COOKIE["user"]) ? $_COOKIE["user"] : "guest" . "!
    ";

提交回复
热议问题