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 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作