I need to cause the back forward and reload buttons to end a web login session and redirect the user to a page or an alert to tell them that they are now logged out. I am not sure how to test for the navigation buttons use with HTML.

Recommended Answers

All 10 Replies

Sessions need some form of server side scripting language to run, and therefore you can't end them with only HTML. What you can do however is have the HTML hyperlink go to a second page, such as a PHP page containing the following:

session_destroy();

What that code would do is remove the session, and then you would use a piece of code to redirect to a second page, or to return to the original page once they've logged out (session has been destroyed).

header('location: /HTML/loggedout.html');

---------------------------------------------------------------------

Main Page

<a href = "/PHP/Logout.php" > Logout </a>

Logout.php

session_start();

session_destroy();

die(header('location: /HTML/Loggedout.html'));

Loggedout.html

<p> You've been logged out! </p>

Ok I have the code to destroy the session and whatnot what I am not sure how to do is cause the user to be directed to said page when they click on the forward, back or reload buttons on the browser?

Unfortunately you have no control over the browser, but what you can include at the top of the page is a check such as:

if(isset($_SESSION['SessionName'])){

    // Logout Code

}

else{
    die(header('location: LoggedOutPage.html'))
}

This shall ask the computer, has the session already been destroyed? If it has then it shall send the user to the page containing the message and if not it shall cause the session to be destroyed.
This way if the user presses the back button, the session isn't attempted to be destroyed twice.

Is this what you mean?

ok so with that code even the page that they are on will not be accessable either right?

I'm getting confused now, do you want to rephrase your original question just to help me out a bit?

ok here is the question that has been posed to me: Please write some brief code that would handle a user’s session on a webpage related to the usage of the back, forward, or refresh buttons. For example if a user were to press one of these buttons in a browser the session would expire and notify the user.

I know how to log a user out, I am just not sure how to send the user to the logout when they use the navigation buttons. If it were just a simple logout I would have used the type of code you suggested to begin with.

Ah right, so it is homework (tut tut)?

I don't think it is possible to implement such a thing, but your best option would be JavaScript. What you could however ask is whether or not the page has changed or been unloaded. However pressing the back/forward/refresh buttons are the same as pressing an in site navigation link so you can't check for a difference, meaning going back and clicking say the home link would equal the same value.

Even if you did find some code, unfortunately it would be very browser specific.

The only way it would work is to check for the page being unloaded or changed.

ok thank you, and as far as the ("tut tut") my instructors always told me that being able to find the answer to questions using resources at your disposal, is just as important as knowing the answer yourself. ;)

Yes, but there is a difference between asking someone else the question and researching an answer to the question.

Anyway, good luck with your project/coursework/lessons.

I reasearc before I just ask others. I use w3shools a LOT

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.