Clear all cookies on page load?

Discussion in 'JavaScript' started by Rezo, Mar 1, 2009.

  1. #1
    Hello,

    I've been trying to find some informations on Google but I couldn't find anything that can solve my problem!

    I need to find a way to clear cookies of the user everytime he loads my page. If anyone knows a way, please share it.

    I posted this in JavaScript section as I heard that this can only be done using JS.

    Thanks in advance.

    Regards,
    Radu
     
    Rezo, Mar 1, 2009 IP
  2. SmartTechCracker

    SmartTechCracker Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You need to use SetCookie function of Javascript and make sure that you dont set the expires attribute so that it will be deleted once after he closes the browser.
    Let me know if this solves your problem
     
    SmartTechCracker, Mar 1, 2009 IP
  3. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #3
    not tested - found this on some site. the idea is sound - i don't think there is a way to do document.cookie = ""; - you need to loop through them all
    function deleteAllCookies() {
        var cookies = document.cookie.split(";");
    
        for (var i = 0; i < cookies.length; i++) {
            var cookie = cookies[i];
            var eqPos = cookie.indexOf("=");
            var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
            document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
        }
    }
    
    
    PHP:
    it may probably be easier in php:
    foreach($_COOKIE as $key => $value)
        unset($_COOKIE[$key]);
    PHP:
     
    dimitar christoff, Mar 1, 2009 IP
  4. siothach

    siothach Active Member

    Messages:
    656
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    60
    #4
    siothach, Mar 3, 2009 IP
  5. nowarningsneeded

    nowarningsneeded Peon

    Messages:
    421
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #5
    also looking for something that works.
     
    nowarningsneeded, Mar 22, 2009 IP
  6. aaron d.

    aaron d. Peon

    Messages:
    37
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    You can only clear cookies set by your website. There is no way to clear other cookies.
     
    aaron d., Mar 22, 2009 IP