Good'ay all,

I'm looking for a method of saving a users session (I.e. automatically logging them back into the same page they logged out from).

But I have a few things to consider:

1) Will they logout or will they just close the browser? This means I can't add the process to the logout script.

2) What if they log back in on another PC? This voids the idea of using cookies.

3) Sessions just won't do the trick imo?

Suggestions? I'm currently considering a SQL query that's executed with the path to the current file at the start of every page and storing it against their name in SQL, then calling on it when they log in. I'm not here for the code just the best method and some insight :)

Any recommendations would be welcomed with open arms :) In the mean time I'm going to flip around google looking for inspiration :3

1) Will they logout or will they just close the browser?

They will close the browser, but you can still trap that with Javascript.

They will close the browser, but you can still trap that with Javascript.

I had no idea you could detect and act upon a browser close in JavaScript! Thank You!! :D

Member Avatar for diafol

Some php/js:

<?php if(isset($_SESSION['login'])){ ?>
<script>
window.onbeforeunload = function()
{
  return confirm('Please logout before closing the browser ?');
}
</script>
<?php } ?>

Something like that in your head area. You could hijack the confirm return value (true) to logout (ajax) the user.

Thanks diafol, I've already found and added the code. It works great, but your suggestion was still really appriciated. Thank you :)

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.