i set cookies to expire at session end by just leaving time out. this works fine if i close the entire browser. but if i just close the tab w my script on it it doesn't expire cookies. I really would like for them to expire even in tabbed browser. any ideas? here's my cookie page:

<?php

// Write the current month and year into cookies and return back to index.php

$month=$_GET['m'];
$mon=$_GET['mon'];
$year=$_GET['y'];
$ti= $_GET['ti'];





setcookie("month", $month);
setcookie("mon", $mon);
setcookie("year", $year);
setcookie("ti", $ti);

header ("location: index.php");



?>

Dani AI

Generated

Session cookies (those set without an expires value) are tied to the browser session, not to an individual tab. That is why sees cookies survive when a single tab closes while the rest of the browser stays open. is right that you can explicitly delete cookies — but the hard part is reliably detecting "tab closed" from the client so the server can be told to expire them.

A practical, reliable pattern is to keep per-tab state in sessionStorage (it is cleared when a tab/window closes) and pair that with your cookie. On page load, if the cookie exists but the sessionStorage token does not, treat the cookie as stale and expire it (either by calling a server endpoint that clears the cookie or by removing it client-side). sessionStorage is per-tab/window, see Window.sessionStorage. Example flow (client-side check + fetch to server):

if (!sessionStorage.getItem('tabToken')) {
  sessionStorage.setItem('tabToken', '1');
  // tell the server to clear the cookie/session for this browser tab
  fetch('/expire_session.php', { method: 'POST', credentials: 'include' });
}

You can also try notifying the server on unload using navigator.sendBeacon, which is more reliable during page unload than sync XHR, but it is not a 100% guarantee in every browser/edge case: Navigator.sendBeacon.

If you need strict per-tab sessions, favor sessionStorage or per-tab tokens over relying solely on session cookies. Also make sure any endpoint that clears sessions is protected (CSRF checks, authenticated requests) so it cannot be abused.

Recommended Answers

All 2 Replies

If you know when your session is done, you can delete the cookie and see if that does it. If the session just dies without warning (and you don't explicitly delete it)then I think that the cookie will hang around until the browser is closed and opens again.

so you dont think there is a way to do it then? I wasnt sure. I dont want to have to manually delete them, because this needs to be a fully self functioning app for use of others as well. It doesnt cause a big flaw in my app, just a lil annoyance is all. it may even be acceptable, was just hoping to make cookies go away even when tab is closed and browser is still running. thanks for your reply :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.