Forum Moderators: coopster

Message Too Old, No Replies

Redirecting to previous page using php

         

kkonline

4:54 pm on Sep 3, 2007 (gmt 0)

10+ Year Member



I want to redirect the user to the previous page, after the processing of the form is successful.

Apart from using meta redirect and java go.back is this possible with php
I don't think i can use header (location mysite/pages.php?a=1&b=2) as it would be a dynamic link .or can i

from current process.php i want to send the user to previous page which is of the format mysite/pages.php?a=1&b=2 (has 2-3 GET parts)

barns101

9:30 am on Sep 4, 2007 (gmt 0)

10+ Year Member



Dynamic URLs are fine to use with header('Location:...') but remember that you can't output anything (such as a confirmation message) to the browser before sending the header.

deMorte

11:20 am on Sep 4, 2007 (gmt 0)

10+ Year Member



You should use:
header("Location: ...");
to redirect.

One way to avoid using relative urls is is to create a config script that defines the basic site url, as such:

define("SITE_URL", "http://www.example.com/mysite/");

And use it as such:

header("Location: <?php print SITE_URL?>pages.php?a=1&b=2");

Of course you have to include the config script first. But at least you have to make only one relative url.

But this is only how I would do this.

omoutop

11:36 am on Sep 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



just an idea - you can use $_SERVER['HTTP_REFERER'] to create a link to send user back to original page

eelixduppy

2:35 pm on Sep 4, 2007 (gmt 0)



>> you can use $_SERVER['HTTP_REFERER']

This is probably the best way to get the job done. This is what is used for sites such as login sites where a visitor might visit a page but they must login first, so they are redirected to the login page, login, and then redirected to the page that they originally visited. The referrer is good for this sort of thing :)

coopster

4:48 pm on Sep 5, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



HTTP_REFERER may not always be present in the $_SERVER superglobal. Two other approaches are to store the <form>'s URI in a hidden input field and use that, or push the REQUEST_URI $_SERVER superglobal value into a $_SESSION variable.