How to redirect to a specific page?

Hi all,
I want to redirect the users like, SitePointForum site…

Means, if a user without login to this site, and clicks a new thread to post his query, it prompts the login page, after successful login, he redirects to the earlier requested page, i.e post new thread page, with user details on top left-corner…

How to implement it.

Can any one help regarding this?

Thanking you…

You could use the super global below and store it in a hidden input field

$_SERVER['HTTP_REFERER']

As SgtLegend said, store the referral URL so you can redirect them to that. If you’re putting it into a hidden form element named “redirectUrl” on a form that POSTs you’d be able to do something like this:


header("Location: " . $_POST['redirectUrl']);
exit();

This will only work if no headers have already been sent, so you might want to check that first, or just make sure that it can’t happen.

You’ll want to do some validation on the redirectUrl value because you shouldn’t trust any data sent to your site (either via POST or SERVER variables, or any other method for that matter).

The exit() command in there just stops any further processing happening further down your script. After sending the location header your script continues to execute any other code in the script - exit prevents that.

Where i want to add this global variable,

$_SERVER['HTTP_REFERER']

In the current page or in login page…?

The current page is posting some data to the next page…, if the user is not logged in, the login page displays. After successful login, it displays the next page,
but my question is:::
Right now the post data is visible in the next page???
If yes, What are the additional thing i want to do…
If no, why, what is the reason?

Thank you SgtLegend…

Can you give me an example code, to do this…

If the login box displays on the same page you can use

$_SERVER['PHP_SELF']

and then when you check the login details simply have the redirect variable stored in a $_SESSION key in case of a login failure.

Something like this should work fine, its not a complete code but its a simple example of the functionality you would need to use.

<form action="login.php" method="post">
    <input type="hidden" name="redirect" value="<?php echo $_SERVER['PHP_SELF']; ?>" />
</form>
session_start();

if (!isset($_SESSION['redirect']) && isset($_POST['redirect']) && !empty($_POST['redirect'])) {
    $_SESSION['redirect'] = trim($_POST['redirect']);
}

//
// FORM VALIDATION CODE HERE
//

//
// SUCCESSFUL LOGIN
//
if (isset($_SESSION['redirect'])) {
    header('Location: ' . $_SESSION['redirect']);
}

Thank you,
but i confused here;
This line of code,i want put in the login page or current viewing page?
because, i have a seperate login page…(if i write this code in login page, $_SERVER holds login page only na…?)

<input type=“hidden” name=“redirect” value=“<?php echo $_SERVER[‘PHP_SELF’]; ?>” />

Also, another problem is;
user enter some details in the current page, then submits the page, after clicking to the submit button, i will check the user is logged in or not.
if not, this will redirects to login page, if the user login now, the previous posting data is posted to the next page or not?

if not, then how to achieve?

Thanking you,