dongyoudi1342 2018-07-05 08:12
浏览 384
已采纳

浏览器窗口关闭PHP时销毁cookie

I have created a popup in WordPress that will open when visitor IP is from Australia. The popup will show up on page load. I want to make it not show until the browser screen is closed.

For this, I used the cookie. I set the cookie when the close button is clicked and creating the cookie in PHP using ajax.

Here is the code I am using to create cookie:

 setcookie("stay_here", "yes", 0, "/");

I set the cookie time to 0 so that it will destroy when the browser closed. The issue is that the site has user account area and when the user is logged in the session of the user is created. This session is not destroying after closing the browser. And when the above cookie created it automatically set expiration time as "Session".

I checked this on firefox and the data variable "Expires" of the cookie is set as "Session". As the session is not destroying when I close the browser and the cookie Expires value which is set as Session is also not destroying.

But I want the only cookie to be destroyed when the browser is closed not the Session.

I hope you understand my issue.

Can you guys look at the issue and provide me a solution to achieve this situation.

Thanks in Advance.

  • 写回答

3条回答 默认 最新

  • dongmi1864 2018-07-05 08:51
    关注

    Ideally cookie created through SETCOOKIE function in PHP with its expire time 0, it will be deleted from browser when you will close the tab and time can't be overwrite with session's cookie time.

    Try to create 1 test.php page and write setcookie("stay_here", "yes", 0, "/"); code and check from browser's cookie information.

    You can also, set session cookie's time to 0 so session cookie will also deleted when browser will be closed.

    ini_set('session.cookie_lifetime', 0);

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 cplex运行后参数报错是为什么
  • ¥15 之前不小心删了pycharm的文件,后面重新安装之后软件打不开了
  • ¥15 vue3获取动态宽度,刷新后动态宽度值为0
  • ¥15 升腾威讯云桌面V2.0.0摄像头问题
  • ¥15 关于Python的会计设计
  • ¥15 聚类分析 设计k-均值算法分类器,对一组二维模式向量进行分类。
  • ¥15 stm32c8t6工程,使用hal库
  • ¥15 找能接spark如图片的,可议价
  • ¥15 关于#单片机#的问题,请各位专家解答!
  • ¥15 博通raid 的写入速度很高也很低