简体   繁体   中英

how to display a message using php and cookies

I need to display a message on my site using cookies.

I tried to use this code :

<?php 
if (isset($_COOKIE['visit']) && $_COOKIE['visit'] == "true"){
  echo 'cookie set, welcome back';
} else {
  echo 'cookie not set, welcome new user';
  setcookie("visit", "true", time()+60*60*24*600);
}
?>

I don't known how to do it, please help!

You need to run setcookie first to get it added to the header. When you run echo first, the header closes and you can't modify it anymore. This is mentioned in the docs of setcookie .

<?php 
if(isset($_COOKIE['visit']) && $_COOKIE['visit'] == "true"){
    echo 'cookie set, welcome back';
}else{
    setcookie("visit", "true", time()+60*60*24*600);
    echo 'cookie not set, welcome new user';
}
?>

What message do you want to display? as the code is working perfectly

you can use the following code.

<?php
if(isset($_COOKIE['visit']==true){
  echo "Write here your message";
}
?>

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