Back Button Refresh

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jaceinla
Forum Commoner
Posts: 25
Joined: Thu Oct 14, 2010 12:57 pm

Back Button Refresh

Post by jaceinla »

Hi everyone,

I'm using Zend but this should be basic PHP.

I have a user that logs in or signs up on a page. When he or she is signed in, he or she is taken to the last page he or she was on. However, when that person clicks the back button, they are right back at the login/sign up screen.

What type of code do I need to use to refresh the page when the back button is pressed so they do not see that sign up page?

Thanks!
Mince
Forum Commoner
Posts: 25
Joined: Mon Aug 03, 2009 9:36 am

Re: Back Button Refresh

Post by Mince »

Well i have an idea that should fix it:

When the user logs in, set a session variable eg: $_SESSION['logged_in'] = true. Then, at the top of the login page, add this:

Code: Select all

<?php
if($_SESSION['logged_in'] == true)
{
    header('Location: http://www.example.com'); // instead of example.com, add your last visited page code or mainpage location
}
?>
Be sure to put that code before any HTML output.
jaceinla
Forum Commoner
Posts: 25
Joined: Thu Oct 14, 2010 12:57 pm

Re: Back Button Refresh

Post by jaceinla »

That's a cool solution and I'll try it out...I've actually already tried several session methods (mainly through Zend) that just do not run when pressing the back button because they are all just referring to cache memory from the browser. Cross your fingers!
Reviresco
Forum Contributor
Posts: 172
Joined: Tue Feb 19, 2008 4:18 pm
Location: Milwaukee

Re: Back Button Refresh

Post by Reviresco »

Code: Select all

<script type="text/javascript">

// onbeforeunload prevents page caching in IE and Firefox:

window.onbeforeunload = function () {
}

// onunload prevents page caching in Safari:

window.onunload = function () {
}

</script>
jaceinla
Forum Commoner
Posts: 25
Joined: Thu Oct 14, 2010 12:57 pm

Re: Back Button Refresh

Post by jaceinla »

Reviresco,

I'm kind of <span style='color:blue' title='I'm naughty, are you naughty?'>smurf</span> that such a perfect workable solution like yours is not easy to find on the internet (actually it was my fault for being close minded to PHP rather than JS). Thank you so much, it's literally plug and play and works!

Mince,

Thanks again for trying to help! It was a great solution but didn't work with the browser.
kalpesh.mahida
Forum Commoner
Posts: 36
Joined: Wed Oct 06, 2010 7:09 am

Re: Back Button Refresh

Post by kalpesh.mahida »

Jaceinla,

I think you were right when user click back button the browser refers to cache output of page.
Client browser can be forced to disable caching using php header() function as bellow.

Code: Select all

header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
Hope this will help you,

Kalpesh Mahida
Post Reply