Thursday, November 6, 2008

How to redirect a page to avoid post data reposting

PROBLEM:

There is a Login page which requires a username and password and sends the data to another page. The page action goes to a page authenticate.php which performs the authentication and if authentication is success, it includes a homepage. when the user logs out from the page and presses a back button on the browser, the browser asks for an alert of reposting the POST data. If this is pressed OK, the user can log in again without giving username and password.


SOLUTION:

We use a redirect method:
This has come helpful in redirecting the page to a home page

The page authenticate.php takes the POSTed values from login.php page. It connects to mysql and then gets the authenticated value. These values are set in session.
say $_SESSION['authenticate']='1';
Now the next command will be to redirect using the header.
header("Location: http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF'])."/homepage.php")

$_SERVER['HTTP_HOST'] will give the location of root directory of Apache server in the local machine.This location is independent of the system that access the server.
dirname is the function  that returns the directory name of the specified file path
$_SERVER['PHP_SELF'] refers to the same file (authenticate.php).
So the header refers to the homepage of the same directory of the authenticate.php page.

There had been an error saying that the headers are already sent and the line was specified. This was due to a blank space left after the end of file of an individual file which was included in the authenticate.php prior to the header function. This leads to sending of blank headers to the browser.

Any queries on the same may be posted to nareshkumar@arijasoft.com

Thank you
Blogged with the Flock Browser

No comments: