Hi all I am new in php,

I want to block the user to return on the previous page?

suppose I have page1 when he/she goto page2 he/she cannot return to page1 (session of page1 expired)

thanks to all.

Recommended Answers

All 12 Replies

Been many years since I used this trick but I remember reading a javascript code where it stops the browser from recording the page in its history making it impossible to go back to unless linked to. I'll try searching for it again but that is one option if the code can be found.

I managed to make a script that will prevent the user from going back to the page. On the pages you do not want to user to be able to go back to place the following script.

<head>
<SCRIPT LANGUAGE=javascript>
function linker() {
history.forward(); 
setTimeout("linker()",2048);
}
var hist=0;
function linkerb() {
if (hist==0) {
    linker();
    }
hist=1;
}
</SCRIPT>
</head>
<body onmousemove="javascript:linkerb();">

That will redirect the user a page forward as soon as the mouse moves over the page.

i've tried your code still I can see the previous page in firefox.
do you have any idea on how to run session of every page then expire it in PHP?

thanks to your help.

i am using this code in my site
its very short and easy

this code dont allow you to go back or forward.

<script language="javascript">
javascript:window.history.forward(1);
javascript:window.history.go(1);
</script>

I have tested your code but it runs only when you click the back and forward button but once you have the link button on the second page to return to page one it will return to page one.

thank you for your help.

off course if you have the link of second page on first one it will definitely go.

this code just dont allow you to go back or forward by using back or forward button.

then what you erectly want?

if you are using sessions on each page then you can apply header function at very first statement of second page.

by using that when your link will go to next page it will execute the first lines and will read the header and come back to first page

what I want is totally block all the previous pages they cannot come back anymore even there is a link on the next2 next3 pages.

thank you sorry for misunderstanding.

i've tried your code still I can see the previous page in firefox.
do you have any idea on how to run session of every page then expire it in PHP?

thanks to your help.

It worked for me but you need to move the mouse after clicking back for the effect to take place and Javascript needs to be enabled. I will see what else I can make.

I did something like this any comment from you guys.

here's my trick.
in the beginning the transaction on page1 

$_SESSION['pageExpired'] is not yet isset meaning empty/null.
isset($_SESSION['pageExpired']; //NO VALUE

in page 1.
I put like this.

if(isset($_SESSION['pageExpired'])=='TRUE') {
	$_SESSION = array(); // destroy all $_SESSION data
	setcookie("PHPSESSID", "", time() - 300, "/");
	session_destroy();
	//clear the previous session and start again
	unset($_SESSION['pageExpired']);
	session_start();
}

in page 2

if(isset($_SESSION['pageExpired'])) {
	echo "<script>alert('page2 session expired.')</script>";
	header("Location: page4.php");
}

in page 3

if(isset($_SESSION['pageExpired'])) {
	echo "<script>alert('page3 session expired.')</script>";
	header("Location: page4.php");
}

page 4 (THE LAST OPEN PAGE anything you can do).

THETRICK!! in page3 set the $_SESSION['pageExpired'])=='TRUE';

so every time they want to go to previous page they cannot continue any transaction because the $_SESSION['pageExpired'] has been set.  :-)

once they proceed to page1...all session destroyed and restart a new session.

and it works! now..

I have found that php itself can't stop the user from viewing the pages as the page is still loaded in the browsers cache. Only javascript can solve this cache problem. I managed to come up with the following just to let you know.

index.php

<?
echo '<html><head><SCRIPT LANGUAGE=javascript>
function linker() {
history.forward(); 
setTimeout("linker()",2048);
}
var hist=0;
function linkerb() {
if (hist==0) {
    linker();
    }
hist=1;
}
</SCRIPT>';
echo '<title>test</title></head><body onmousemove="javascript:linkerb();">';
?>
<a href="redirect.php?url=protectedexamplepage.php">Next page</a>

redirect.php

<?
if (isset($_GET['url']) && !empty($_GET['url'])) {
    setcookie('page',$_GET['url'],(time()+86407));
    header('Location: '.$_GET['url']);
    } else {
    echo '<h1>Page not found</h1>';
    }
?>

protectedexamplepage.php

<?
if (!isset($_COOKIE['page']) || substr_count($_SERVER['PHP_SELF'],$_COOKIE['page'])==0) {
    header('Location: redirect.php');
    }
echo '<html><head><SCRIPT LANGUAGE=javascript>
function linker() {
history.forward(); 
setTimeout("linker()",2048);
}
var hist=0;
function linkerb() {
if (hist==0) {
    linker();
    }
hist=1;
}
</SCRIPT>';
echo '<title>test</title></head><body onmousemove="javascript:linkerb();" bgcolor=#FFCCCC>';
?>
<a href="redirect.php?url=index.php">index</a>

my trick! is working fine now...
I haven't try your last code...

thank you for your help I will try later....

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.