Prevent back button after logout - php

I don't want the user to go back to secured pages by clicking back button after logging out. In my logout code, I am unsetting the sessions and redirecting to login page.But, I think the browser is caching the page so it becomes visible despite the session being destroyed from logout.
I am able to avoid this by not allowing the browser to cache
header("Cache-Control", "no-cache, no-store, must-revalidate")
But this way I am loosing the advantage of Browser Caching.
Please suggest a better way of achieving this. I feel, there must be a way of handling this by javascript client side

Implement this in PHP and not javascript.
At the top of each page, check to see if the user is logged in. If not, they should be redirected to a login page:
<?php
if(!isset($_SESSION['logged_in'])) :
header("Location: login.php");
?>
As you mentioned, on logout, simply unset the logged_in session variable, and destroy the session:
<?php
unset($_SESSION['logged_in']);
session_destroy();
?>
If the user clicks back now, no logged_in session variable will be available, and the page will not load.

I was facing this same problem and spent whole day in figuring out it,
Finally rectified it as follows:
In login validation script if user is authenticated set one session value for instance as follows:
$_SESSION['status']="Active";
And then in User Profile script put following code snippet:
<?php
session_start();
if($_SESSION['status']!="Active")
{
header("location:login.php");
}
?>
What above code does is, only and only if $_SESSION['status'] is set to "Active" then only it will go to user profile , and this session key will be set to "Active" only if user is authenticated... [Mind the negation [' ! '] in above code snippet]
Probably logout code should be as follows:
{
session_start();
session_destroy();
$_SESSION = array();
header("location:login.php");
}
Hope this helps...!!!

Here's an easy solution which I have used in my application.
Add the below code inside script tag in the login HTML page (or whichever page it redirects to after logout)
<script>
history.pushState(null, null, null);
window.addEventListener('popstate', function () {
history.pushState(null, null, null);
});
</script>
It will disable the back button. You will not be able to go back by clicking on the back button.
Note: Not tested on Safari.

I think your only server side option is to disallow caching. This is actually not that bad if you are using a Javascript heavy application as your main HTML might only be a series of JS calls and the Views are then generated on the fly. That way the bulk of the data (JS MVC and core code) is cached but the actual page request isn't.
To add to the comments pasted below I would suggest adding a small AJAX call during load time that fires even for cached pages that goes to your backend and checks the session. If not session is not found it would redirect the user away. This is clientside code and not a secure fix, sure, but looks nicer.
You could get this off your conscience with
A cheap fix if all else fails would be a "Please close this window for security reasons" message on the logged out page. – izb May 9 '12 at 8:36
But like N.B. said
You don't have to disable anything. If they go back, they're served the cached version of the restricted page. If they try to click around it, nothing will work because the appropriate session won't be set. – N.B. May 9 '12 at 7:50

You could insert a condition/function on each restricted page, checking if the appropriate session variable is set or not. This way, you can print 2 versions of the page (one for the valid users, and one redirecting to the login page)

Avoiding the user to go back is not a good reason and most of all not secure at all.
If you test the user's session before every "admin" action made on the website, you should be fine, even if the user hit the back button, sees the cached page and tries something.
The "ties something" will return an error since the session is no longer valid.
Instead, you should focus on having a really secured back office.

Here's an easy and quick solution.
To the login form tag add target="_blank" which displays content in a different window. Then after logout simply close that window and the back button problem (Safari browser) is solved.
Even trying to use the history will not display the page and instead redirect to login page. This is fine for Safari browsers but for others such as Firefox the session_destroy(); takes care of it.

the pages on which you required loged in, use setInterval for every 1000 ms and check wheather user is logged in or not using ajax. if user session is invalid, redirect him to log in page.

Note that although users can't change anything after resetting session data and/or cookie, they still may see usual information accessible to a logged in user as they appeared on the last visit. That is caused by browser caching the page.
You have to be sure to add the header on every page accessible by a logged in user, telling the browser that the data is sensitive and they should not cache the script result for the back button. It is important to add
header("Cache-Control: no-cache, must-revalidate");
Note that those other elements other than the immediate result of the script under this header, will still be cached and you can benefit from it. See that you gradually load parts of your page and tag sensitive data and the main HTML with this header.
As the answer suggests, unsetting the logged_in portion of $_SESSION global variable can achieve logging out, but be aware that first, you don't need to destroy session as mentioned in the PHP's session_destroy() documentation
Note: You do not have to call session_destroy() from usual code. Cleanup $_SESSION array rather than destroying session data.
And second, you better not to destroy the session at all as the next warning on the documentation explains.
Also, unset() is a lazy function; meaning that it won't apply the effect, until next use of the (part of the) variable in question. It is good practice to use assignment for immediate effect in sensitive cases, mostly global variables that may be used in concurrent requests. I suggest you use this instead:
$_SESSION['logged_in'] = null;
and let the garbage collector collects it, at the same time it is not valid as a logged in user.
Finally, to complete the solution, Here are some functions:
<?php
/*
* Check the authenticity of the user
*/
function check_auth()
{
if (empty($_SESSION['logged_in']))
{
header('Location: login.php');
// Immediately exit and send response to the client and do not go furthur in whatever script it is part of.
exit();
}
}
/*
* Logging the user out
*/
function logout()
{
$_SESSION['logged_in'] = null;
// empty($null_variable) is true but isset($null_variable) is also true so using unset too as a safeguard for further codes
unset($_SESSION['logged_in']);
// Note that the script continues running since it may be a part of an ajax request and the rest handled in the client side.
}

Related

if session is not set - redirecton doesn't work

index.php
session_start();
if (!(isset($_SESSION['admin']))) {
header ('Location: login.php');
}
I want to redirect a user if it's not loged in.
After login (without remember me), I turn off the browser (Chrome) and turn it on again.
All sessions should be removed, so I expect a redirection to login.php, but it doesn't work (index.php is loaded).
If you see a session as "a browser session", then this is surprising behaviour. But this is not the case.
A session is a session as defined by the server. To remember that this is that same user, it saves the session as a cookie. For the point of view of the server, it doesn't really matter if you close your browser, shut down your computer, or drink a cup of coffee: you are still that same, unique, person, so your session should be the same.
As long as your cookies are saved AND are not too old, it's all the same session. You could, from the user side, try to instruct your client to stop this, for instance on a shared account: have the browser remove all cookies on exit or use different profiles (this at least is possible in chrome).
So the expected behaviour is that as long as the cookie is valid, the session is the same. Cookie validity (or actually: removal) CAN be tied to closing your browser, but most of the time it isn't. I am not sure it is even possible to directly detect if a browser was closed, so it's hard if not impossible to force your described behaviour from the server side.
edit: a quick addition from the manual:
Session support in PHP consists of a way to preserve certain data
across subsequent accesses. This enables you to build more customized
applications and increase the appeal of your web site. All information
is in the Session reference section.
It talks about "subsequent accesses", which is quite broad.
You need to use session destroy function and then check. See the code below:-
session_destroy();
if (!(isset($_SESSION['admin']))) {
header ('Location: login.php');
}
or an alternative is to use unset function then check:-
unset($_SESSION['admin']);
if (!(isset($_SESSION['admin']))) {
header ('Location: login.php');
}
Well to destroy the session you can use an ajax call on browser unload event:-
$(window).unload(function() {
$.get('session_destroyer.php');
});

how to clear login session in php

Please help me out to clear login session.
For example
If I am a user of a particular & I want to check my updates. So I used my user id & password to login to the particular page.
After checking my updates I logged out from the page. After logging out, I used back button in the browser to go back to previous session.
Automatically enters into my page without giving any login details. To prevent from entering into page without any login details the session should be cleared. So help me out to clear the session.
This is a client side problem. If you press back button in browser, the browser loads the previously stored cached page.
Better way is, You should write a javascript function in login page which invokes logout.jsp whenever loaded first time or through back button.
You need to set the cache control header on your pages to ensure they are not cached. Since Have a look at the following question which goes into detail about how to set the cache header correctly in different languages - How to control web page caching, across all browsers?
To clear session in php, you can call a script logout.php containing the following :
<?php
session_start();
$_SESSION = array();
// PHPSESSID is default name, but you may have a custom.
setcookie("PHPSESSID", '', time() - 1, '/');
}
See a more complete example at
http://fr.php.net/session_destroy

SESSION destroys after POST

I'm developing a site for someone where users can post problems to a website and the Admin of the company can view the problems and give a solution for it. I use one page that takes care of the login handling and a mysql db. The problem is that i can log in, it shows me another panel(userpanel), but whatever other button i click, it takes me back to the login panel.
It used to work as i was able to post data to my database. but suddenly after some changes on my website, it stopped working (and i can't find the problem anymore.)
When i log in, $_SESSION["LoggedIn"] gets a value and goes to the other panel on the same page with http post. when i click a button there, it seems that $_SESSION["LoggedIn"] is removed again because i check with isset if the user is logged in, otherwise it shows the userpanel.
//check user logged in
if (isset($_SESSION['LoggedIn'])) {
//Problem posted
if (isset($_POST["plaatsen"])) {
//Processing - plaatsen
postProblem();
}
} else {
//do login thing
}
I've attached my code here and i hope anyone can help me out.
Index.php: http://pastebin.com/BZSirUTT
Functions.php: http://pastebin.com/7Hknhm9r
Website: http://php.olvgroeninge.be/~sac.26A-07/php/Oefeningen/Oefening3/index.php (it's in dutch)
Sessions typically don't disappear by themselves. If they do, assuming you did run session_start() first, it can be due to:
The session could not be saved on the server; this can be due to disc space or permission issues. It could also be due to any page output before the session_start() statement. Fortunately, you can see this by heightening the error reporting at the start of your script:
ini_set('display_errors', 'On');
error_reporting(-1);
The session could not be found; for session to perpetuate it requires the session id at every request. Depending on your settings, this can come from the URL (PHPSESSID=xxx) or cookies. In the latter case, you can verify that your browser sends the cookie by whipping up the browser developer tools.
You destroyed the session yourself; calling session_unset or session_destroy will clear and remove the session respectively. Make sure this doesn't happen accidentally.
The session is garbage collected; this normally only happens after some time of inactivity, configured using the relevant ini settings
The session could not be read; just like #1 but for reading.
Hope at least one of these points helps you.
Debugging the session
You can add the following code to all pages to isolate the problem:
echo '<pre>', htmlspecialchars(print_r($_COOKIE, true)), '</pre>';
session_start();
echo '<pre>Session = ', session_id(), '</pre>';
Update
The problem is that the index.php doesn't set any cookies; OP created a separate small test page which does set cookies. Turns out the problem is #2 then :)

How can I reset session when user navigates away from page?

I've created a session to keep track of some user actions on a specific page.
When user navigates away from that specific page (but still on the same site), I need to reset the session.
I can set set timer for when session expires, but that's not what I want.
How can I reset session on page navigation?
Just make resetting the session the very first thing you do when loading a new page.
Do you have a single page that all traffic is routed through? If so you could do a simple check
if ($current_page != $session_page) {
$_SESSION = array();
}
If you don't have a single page all traffic goes through, then you will need to reset the session at the beginning of every other page.
$_SESSION = array();
Alternatively you can use
unset($_SESSION);
or
session_destroy();
Though note these handle slightly differently to the suggestions above. Note the second (session_destroy) will require you restart the session should you need it again.
You can never be sure that the session is really destroyed.
But there is an old trick used in for example chat applications where a "... is leaving" message is to be printed: On unload just open a popup windows with window.open() that calls your logout page and then closes itself.
What I've done in the past is make a quick ajax call on unload to an unset page, much like the other answers on this page.
<script>
function myAjaxCall(){
//if using jquery
$.ajax('unset.php');
}
</script>
<body onunload="myAjaxCall()">

Incorrectly redirecting user back to a PHP page after submitting a form

All,
This question probably has a very simple answer - something I'm overlooking. But maybe someone can tell me where to look...
I have a PHP page ("index.php") with a very simple login form (e.g., username and password).
When the user clicks the "Submit" button, the form POSTs the values to another PHP page ("login.php"). That page is supposed to confirm the user's credentials, then do the following:
If the user's credentials are not
correct, redirect the user to
error.php, along with an error
message
If the user's credentials ARE
correct, create a session and set $_SESSION['authenticated'] = true, then redirect him to "loggedin.php"
[UPDATE]
Then, on loggedin.php, I check to see that isset($_SESSION['authenticated']) returns true. If it does, then proceed. If not, redirect the user back to index.php.
However, here's what happens. The FIRST time I fill out the form (with valid creds) and submit it, I can see briefly in the URL bar that the user is sent to login.php, then loggedin.php, but then BACK to index.php.
But, if I re-enter the same credentials and submit the info a SECOND time, everything works exactly as it should.
So, in short, it looks like either login.php is not setting the $_SESSION variable the first time through, or that it is, but for some reason, it's not set when I check it for the first time on loggedin.php
Is there some delay between setting the variable on login.php, and having isset() return true on loggedin.php?
Or, is there something else I'm doing wrong?
Here are the relevant (I think) snippets of code:
In login.php:
session_start();
$_SESSION['authenticated'] = true;
header('Location: http://www.mydomain.com/loggedin.php');
In loggedin.php:
session_start();
$authenticated = $_SESSION['authenticated'];
if (!isset($authenticated)) {
header('Location: http://www.footballpoolz.com/mobile/index.php');
die();
}
Many thanks in advance for any advice or insights!
Cheers,
Matt Stuehler
I think I may know the cause of the error. The session has to be linked to the browser and the IP address (this way more than one person can be logged in at a time). This means that the session has to not only be stored server-side, but the client has to have a link to the session as well so you know who they are logged in as when they request data. This session id is shared as part of the header during all HTTP requests.
When you're redirecting the user, though, you aren't giving them a chance to send new headers, are you? You're probably just sending them the new page. This new page never saw a header from them, so it doesn't know which session variable (PHP has hundreds or even thousands of session variables) belongs to them. When you log back in a second time, you are sending a header, and thus you're sending the session ID and PHP knows which session variable is yours.
There are two solutions. The first is to find a way to redirect them that forces them to send a new header. I believe using header("Location: www.mysite.com/newpage.php"); will do this. I may be mistaken.
The alternative is to temporarily pass the session id when you redirect them to loggedin.php so that you know they are logged in for that first page load. After the initial page load, you no longer need to take this extra step since it will be done for you every time they request a page. To pass the session id you just append ?SID=... to your redirect.
http://www.php.net/manual/en/session.idpassing.php
Redirects really slow things down and cause extra server load. What you should be doing is posting back to the index.php page, which will detect if there is a POST or not. Then log the user in and display the contents of the loggedin.php file. No redirects necessary.
After all, you already know that the user is validated, why redirect them to another page where you have to check validation again (which you just did)? This is more of the concept of a "Front Controller" where your index.php acts as a router to load and display different pages. Even if it's just a welcome page when they login. This eliminates any issues with delays.
You are doing a session_start, right?
Instead of using this true . Try to put some value.
like $_SESSION['username']='mattstuehler'
and check
$loggeduser=$_SESSION['username'];
if(!empty($loggeduser))
I dont see any bugs anyway

Categories