creating a redirect

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rathlon
Forum Commoner
Posts: 45
Joined: Sun Nov 10, 2002 8:07 pm

creating a redirect

Post by rathlon »

I have a simple update form for my database where users view the current info that is loaded and then update whatever they want via an html form. If the user updates info I have it go to a results page showing what was changed. How do I redirect the user back to the page where he or she was before, using php?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

with header('Location: '.$redirectURL); a 302 redirect is sent to the client.

Server variables: $_SERVER
'HTTP_REFERER'
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.
you may use this to track the client's origin.
rathlon
Forum Commoner
Posts: 45
Joined: Sun Nov 10, 2002 8:07 pm

Post by rathlon »

since I'm not too abreast of the multiple facets in the php language, I'll assume that your speaking of using sessions here?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

not necessarily. e.g. you may also ship the url as hidden field within a form ;)
rathlon
Forum Commoner
Posts: 45
Joined: Sun Nov 10, 2002 8:07 pm

Post by rathlon »

yeahhhhhhhhhhhhhhhhhhhhhhhhhh...............well, I must be totally retarted b/c I tried that whole

<input type="hidden" name="redirect" value="http//path/to/my/page.php">

and ahhhhhhhhh......well...no go.

?
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

Code: Select all

<?php
// do some stuff
header("Location: $SERVER&#1111;'HTTP_REFERER']");
?>
try that
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

that will lead directly to the page that generated the form (except the parse error because of the inner literal ;) )
I doubt that will be sufficient (but it may be)

<input type="hidden" name="redirect" value="http//path/to/my/page.php">
if you're not including the server such as http://my.server/path/to/my/page.php skip the leading http. The processing script must not output any content other than header information (also see: Sticky: Before Post Read: Warning: Cannot add header information)

assuming your form's method is "POST" try

Code: Select all

// &lt;-- some processing here but no output --&gt;
header('Location: '.$_POST&#1111;'redirect']);
if uncertain print the passed values with

Code: Select all

&lt;?php echo '&lt;pre&gt;', print_r($_POST); echo '&lt;/pre&gt;';
instead of your original script.

the $_POST array was introduced in php 4.1.
Check your current version with

Code: Select all

&lt;?php phpinfo(); ?&gt;
Post Reply