dsxpt62448 2013-05-25 21:34
浏览 461
已采纳

Cookie的正确日期格式

So I am trying to do what I think is the impossible. I want to destroy all session cookies on browser or tab close, so I came up with a solution, I am just having small issues with JavaScript formatting. This does have to be time based, as when the next page reloads, it will put the timer for expiration for a year, so that other pages on my site don't delete the cookies as well. So until they navigate away from the site or close the tab, the cookie wont expire.

So The JavaScript starts like this:

var today = new Date(); 
today.setSeconds(today.getSeconds() + 5);

alert(today);



window.onunload = function(){
   document.cookie = 'PHPSESSID=; expires=' + today;
   };

So When I run this, it does not recognize the today variable, or I am not formatting today correctly.

All help is appreciated!

  • 写回答

2条回答 默认 最新

  • doumen5895 2013-05-28 14:44
    关注

    So I decided to destroy the session cookies with a small script.

    This is what I came up with.

    window.onunload = function(){
      var today = new Date();
      today.setSeconds(today.getSeconds() + 2);
      var today1= today.toUTCString()
    
    
    
      document.cookie =
          'PHPSESSID=<?php echo $sesid; ?>; expires='+ today1 +'; path=/'
          alert(document.cookie);
      };
    

    So This will set the destroy to 2 seconds, but if you navigate to any other page on my site, it will run this first.

    var today2 = new Date();
    today2.setSeconds(today2.getSeconds() + 10000);
    var today3= today2.toUTCString()
    
    document.cookie =
      'PHPSESSID=<?php echo $sesid; ?>; expires='+ today3 +'; path=/';
    

    This allows the session to be destroyed anytime you navigate away from the page.

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

报告相同问题?

悬赏问题

  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python