user638009
user638009

Reputation: 223

Create unset cookie button

I have created a cookie using php and now I need to create a button the user can click to log out.

This would be the php code to unset the cookie:

 unset($_COOKIE['access_token']);  

But I'm a quite stuck with how to make the button functionality. Can anyone help me with it?

Thanks a lot

Upvotes: 1

Views: 8218

Answers (3)

Xaero Degreaz
Xaero Degreaz

Reputation: 1085

It's actually best to do:

setcookie("myCookie", null, 1); //# This will expire / delete *immediately*

If you use

setcookie("myCookie", null, 0), the cookie is still valid until they close their browser.

Upvotes: 1

Shakti Singh
Shakti Singh

Reputation: 86476

By the way you can delete cookie by setting time in past

setcookie($_COOKIE['access_token'],'',time()-3600);

you should unset session

session_destroy()'

Upvotes: 1

dobs
dobs

Reputation: 2764

if(isset($_GET['logout'])) {

unset($_COOKIE['access_token']);

header('Location: /');

exit;

}

on site

<a href="?logout">logout</a>

Upvotes: 1

Related Questions