ignore back button or refresh button  on the browser

Posted on 2006-04-03
Last Modified: 2008-02-01

I have

index.php, a.php, b.php, c.php, d.php, e.php

I am in d.php now. if i click back button or refresh button on browser, it has to go back to a.php
And also, if I click back button or refresh buttonon browser, it has to go back to a.php.

How can i do that??

Question by:bbvic
    LVL 1

    Expert Comment

    The "and also" part to your question and the first part to your question are the same thing. Did you mean to add somethings else there?

    Also, do you only need to get back to a.php when coming (back or reload) from d.php or from all of them?

    Author Comment

    from all of them..
    LVL 6

    Accepted Solution

    'index.php, a.php, b.php, c.php, d.php, e.php'

    So the first time you display a page, you want it to display normally, but the second time you display a page, you want it redirected to a.php.

    If someone requests b.php, they see b.php, but if they reload the page, they see a.php.

    If someone requests b.php, they see b.php, if they click on a link to c.php, they see c.php, if they then click 'back', they see a.php (not b.php).

    Effectively, a visitor can only see b.php once.  As soon as they leave the page they can't get back to it; ever.  If they reload the page, they are sent someplace else.

    First of all, there is no solution that you can implement that can't be foiled by malicious users.  Ultimately, you will need some way to identify returning users and users can always spoof their identification.

    Some browsers will allow you to tamper with the history by using Javascript.  You may be able to embed some Javascript to take care of the user trying to go 'back'.

    Another approach might be to use $_SERVER['HTTP_REFERER'].  However, it can be spoofed and I don't know what browsers do with the referer header when you reload a page.

    What ever you do, you probably want to make sure that none of your pages are cached by doing the following:

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

    You could then set a cookie on the visitors machine after they visit each page (other than a.php):

    if (isset($_COOKIE['beenHere']))
    /* set the cookie for 30 days (60s*60m*24h*30d) */
    setcookie('beenHere', 'true', time()+60*60*24*30);

    Alternately, you could structure it more like this:

    if (!isset($_COOKIE['beenToB'] && isset($_REQUEST['B'])
        setcookie('beenToB', 'true', time()+60*60*24*30);

    Then use links like:

    Instead of:

    You chould use IP addresses and some kind of time-out instead of cookies.  The time-out would be necessary to account for DHCP.  Note, this technique wouldn't be as fool-proof but it would be harder for malicious users to spoof.
    LVL 6

    Expert Comment

    The best solution will probably be to always send users to a.php every time and then 'require' the other pages as needed.  This gives you the most control and the only URL that ever gets stored in their history is a.php.  The user doesn't ever need to see the direct URL to the other pages (b.php, c.php, etc.).
    LVL 9

    Assisted Solution

    try to work out with the code below

    <script language="javascript">
    function onUnloadJS(){
    return true;

    <body onunload="onUnloadJS()">


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Easy Project Management (No User Manual Required)

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    As this topic comes over and over again in different forms, I've finally decided to write a short (yea, right...) article / tutorial about pagination with PHP with MySQL database. There are dozens of these kind of tutorials, I know - I wanted to mak…
    A colleague recently asked me about how to give his client a small part of the web site that could be completely under the client's control.  Since I have done this sort of thing before to add emergency banners to a web site, I decided I would creat…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now