{"id":111,"date":"2009-06-23T11:47:46","date_gmt":"2009-06-23T11:47:46","guid":{"rendered":"http:\/\/nicholshayes.myzen.co.uk\/blog\/?p=111"},"modified":"2009-06-23T11:48:12","modified_gmt":"2009-06-23T11:48:12","slug":"deleting-session-and-cookie-entries","status":"publish","type":"post","link":"http:\/\/nicholshayes.co.uk\/blog\/?p=111","title":{"rendered":"Deleting Session and Cookie entries"},"content":{"rendered":"<p>I&#8217;ve had a problem with clearing a users session and cookie information if they failed log on. In the past I&#8217;ve simply used:<\/p>\n<pre>\r\n  def clear_session\r\n    session[:user_id] = \"\"\r\n    session[:user_hash] = \"\"\r\n    cookies[:user_hash] = \"\"\r\n  end\r\n<\/pre>\n<p>But this often fails to clear the setting when I tested in Firefox. I tried to find an option to delete a session or cookie entry but could not get either session.delete&#040;:name&#041; or session[:name].delete (which I found references to) to work.<\/p>\n<p>So I&#8217;ve used a pragmatic approach to the problem. This works:<\/p>\n<pre>\r\n  def clear_session\r\n    session[:user_id] = \"dead\"\r\n    session[:user_hash] = \"dead\"\r\n    cookies[:user_hash] = { :value => \"dead\", :expires => 1.minute.ago }\r\n  end\r\n<\/pre>\n<p>It&#8217;s not pretty, but it works. It seems that at least for Firefox, you need to pass something to the browser cookie (session after all is a glorified cookie) to over-write it. Passing it nothing can leave the old entry there.<\/p>\n<p>Of course you need to pass it something that your application will find invalid as an entry for that value. For example User.find&#040;&#8217;dead&#8217;&#041; will return nil.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve had a problem with clearing a users session and cookie information if they failed log on. In the past I&#8217;ve simply used: def clear_session session[:user_id] = &#8220;&#8221; session[:user_hash] = &#8220;&#8221; cookies[:user_hash] = &#8220;&#8221; end But this often fails to &hellip; <a href=\"http:\/\/nicholshayes.co.uk\/blog\/?p=111\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/111"}],"collection":[{"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=111"}],"version-history":[{"count":4,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/111\/revisions"}],"predecessor-version":[{"id":115,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/111\/revisions\/115"}],"wp:attachment":[{"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=111"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=111"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/nicholshayes.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=111"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}