Saint Robson
Saint Robson

Reputation: 5525

PHP Redirect Back To Where User Was

Suppose I have this script :

http://www.example.com/profile.php?uid=12345

And inside this script, there's a send message feature which is handled by sendmessage.php. What sendmessage.php actually does is to INSERT something into the database then redirect back to where user started (profile.php?uid=12345).

I tried to use header("Location: profile.php").

But since profile.php requires ?uid=12345 in the URL [and it's dynamic], my redirect plan is not working.

How do I 'pass' the current URL. In my case 'profile.php?uid=12345' to sendmessage.php using the GET method. And then, once sendmessage.php has completed processing, send the user back to their own screen (profile.php?uid=12345)?

Thanks in advance.

Upvotes: 1

Views: 484

Answers (6)

kewlashu
kewlashu

Reputation: 1109

In your profile.php?uid=12345 if you are using form to send data to sendmessage.php then you can have a hidden field like -

<input type="hidden" name="redirect_to" value="<?=$_SERVER['REQUEST_URI']?>">

Then in sendmessage.php you can use header("Location: ".$_REQUEST['redirect_to']);'

Upvotes: 2

Eric
Eric

Reputation: 1

You could use GET and some javascript. Using this method avoids the issues with using PHP's header function.

echo "<script type=\"text/javascript\"> window.location = \"profile.php?uid=" . $_GET["uid"] .    "\"</script>";

Make you sure check that the "uid" is actually passed. You could also store the uid in a session variable when the user first logs on, and then use that in my example thus avoiding having to use GET. Either way works.

Upvotes: 0

Richard Dev
Richard Dev

Reputation: 1170

you should pass the variable to the sendmessage.php like

sendmessage.php?uid=<?php echo (isset($_GET['uid']) ? (INT)$_GET['uid'] : 'default') ?>

Then on the sendmessage.php. Use something like

header("Location: profile.php?uid=" . (isset($_GET['uid']) ? (INT)$_GET['uid'] : 'default') ); 

Upvotes: 0

JayDM
JayDM

Reputation: 1186

Another option is to make sure that you send the uid as part of the form submission data.

Then you can reconstruct the URL using the passed in value:

header("Location: profile.php?uid=" . $_GET["uid"]);

Or, store the user ID in their session. That way it is always available in $_SESSION["uid"] and you won't have to remember to make an otherwise extraneous field in your forms.

Upvotes: 0

Oliver Spryn
Oliver Spryn

Reputation: 17358

Try this:

header("Location: " . $_SERVER['HTTP_REFERER']);

Note from PHP.net:

The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted.

Upvotes: 2

Kristian
Kristian

Reputation: 21830

one trick is to populate the information in the link itself, so that way when you go to a different page, those params will still be there... if you so desire, you can use a param as a previous url.

Upvotes: 0

Related Questions