delete cookies

leontager

Well-known member
Joined
Jun 17, 2003
Messages
89
how can I clear the cookies from internet explorer. is there any built in function or do I have to scan for each file? if so, could you please provide me with sample code. If possible please make it as simple as possible since I am new:D
 
this should do the job ...
Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim cookiePath() As String = Directory.GetFiles("C:\Documents and Settings\den\Cookies") /// path to your cookies folder must go here.
        Dim x As Integer
        For x = LBound(cookiePath) To UBound(cookiePath)
            If cookiePath(x).Substring(cookiePath(x).Length - 4, 4) = ".txt" Then
                File.Delete(cookiePath(x))
            End If
        Next
    End Sub
hope it helps :)
 
If you dont want to hardcode the cookie path, since it can be different on other computer you can use this to retrieve the path to the cookies folder:
Code:
System.Environment.SpecialFolder.Cookies
:)
 
although when i used System.Environment.SpecialFolder.Cookies i got an error, path not found ( pointing to the apps path / bin ) , thats why i pointed to the actuall name. not sure whats going on there.
 
This is how you use it:
Code:
Dim files() as string = IO.Directory.GetFiles(System.Environment.GetFolderPath(Environment.SpecialFolder.Cookies))
You need to use the GetFolderPath() method becuase SpecialFolder
is just an enumaration.
:)
 
ive done it abit differently. I should have posted earlier. I did something similar to waht dynamic_sysop suggested.

im f As IO.FileInfo
Dim dir As New IO.DirectoryInfo("C:\Documents and Settings\user1\Cookies\")

For Each f In dir.GetFiles
If f.Extension = ".txt" Then
IO.File.Delete(f.FullName)
End If
Next
I will change it to what you added mutant. Thanx everybodys replys. I appriciate it very much :-)
 
Code:
Dim f As IO.FileInfo
Dim dir As New IO.DirectoryInfo("C:\Documents and Settings\user1\Cookies\")

For Each f In dir.GetFiles
If f.Extension = ".txt" Then
IO.File.Delete(f.FullName)
End If
Next

will this happen on the Client side? or on the Server? I mean.. will this delete the cookies on the client machine?
 
Request.Cookies.Clear()

If a website were able to remotely delete all of a clients cookies this sounds like a bit of a security flaw, and even if it is possible I would advise against it. You can delete all of a clients cookies for the current site by using the Request.Cookies collection. You can also use this collection to add, modify, inspect and delete individual cookies.

Code:
Request.Cookies.Clear() Clear all cookies

Good luck :)
 
MrPaul said:
If a website were able to remotely delete all of a clients cookies this sounds like a bit of a security flaw, and even if it is possible I would advise against it. You can delete all of a clients cookies for the current site by using the Request.Cookies collection. You can also use this collection to add, modify, inspect and delete individual cookies.

Code:
Request.Cookies.Clear() Clear all cookies

Good luck :)

this will delete only MY cookies on the CLient Side?

I mean, I want my website to be able to display something like "All cookies cleared." message which of course would mean all the cookies that I save on the clients computer has been deleted.
 
Cookie deletion...

Apologies, I replied without really thinking. The code above will clear the Cookies collection and so prevent any further cookies from being set on the client. It will not affect cookies already on the client. To do that, each cookies expiration time must be modified:

Code:
For i As Integer = Request.Cookies.Count - 1 To 0 Step -1
    Dim c As New HttpCookie(Request.Cookies(i).Name)
    c.Expires = DateTime.Now.AddDays(-1D)
    Request.Cookies.Add(c)
Next

Or

C#:
for (int i = Request.Cookies.Count - 1; i >= 0; i--) {
    HttpCookie c = new HttpCookie(Request.Cookies[i].Name)
    c.Expires = DateTime.Now.AddDays(-1d)
    Request.Cookies.Add(c)
}

Good luck :)
 
Thank you very much MrPaul..

But what if I want to delete the cookies when a certain event is triggered?

I have this:

Code:
function resetStatus()
{
var abssize = document.body.offsetWidth-30;
if((event.clientY < 0 && event.clientX >= abssize) || (event.clientY < 0 &&
event.clientX >= 0))
{
alert("The page will now close.");
window.open("../forceLogout.asp", "_blank");
}
}

and forcelogout.asp does a bit of "garbage collection" which SHOULD include the deletion of cookies because I have to force a logout because the user closed the browser "ilelgally" by not clicking my "logout" link.
 
Code in forceLogout.aspx

If the user closes their browser window, all non-persistent cookies are automatically deleted, as they are assigned to a specific browser window. If you want to delete persistent cookies, then you would put the code from my previous post into forceLogout.aspx. It may be possible to delete cookies using client-side Javascript, but it is certainly not the standard approach.

:)
 
guess what..

I got clearance from the admin to delete ALL cookies in all terminals.. :D
(someone jsut handed me a mallet!)

it seems developers here have had problems with cookies and trapping the X button click for quite some time..

thank you very much MrPaul and im happy you liked my forcelogout code.
 
Back
Top