redirects in php

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
rk
Forum Newbie
Posts: 1
Joined: Thu Jan 23, 2003 5:20 am

redirects in php

Post by rk »

I am a coldfusion developer and in CF there is the nice <cflocation url=""> tag which lets you redirect anywhere from the page to another location. Great for refreshing pages and so after performing a form action. But in PHP I only came across the function to send a http header, before any code is executed. Is it possible to make redirects after for example a query is executed?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You can use the header() function to redirect the page before anything is output to the browser - so it can be used after some code has been executed.

This may be of interest:
viewtopic.php?t=1157

Mac
elipollak
Forum Newbie
Posts: 22
Joined: Sun Jan 19, 2003 10:23 pm

Post by elipollak »

I had to do some coldfusion work for a while ...
ugh ... it made me appreciate php even more than I already did.

Anyway, in php, if you have a form with submit button name = "send", then u can have in your php script :

Code: Select all

if ($send) &#123;
   // do stuff with info you got

  // then 
    print " ... successful or woteva";
    print"<a href=........>click here to go to some other page</a>";
&#125;
else &#123;
   // print out the form itself :

   print "<form action="" method="post"> 
   // note   action=""   says post back to this page

   //........ rest of form
&#125;
so it can all be in the same file - the form and the action perfomed on the form.

hope this is of some help
User avatar
puckeye
Forum Contributor
Posts: 105
Joined: Fri Dec 06, 2002 7:26 pm
Location: Joliette, QC, CA
Contact:

Post by puckeye »

You can always use the META TAG refresh to redirect to another page...

But I think twigletmac is right about using header().

I use this all the time to prevent users from reloading the form processing page. If your form doesn't check out youcan output some warnings and reprint the form with valid fields already filled otherwise you can write a header ("Location:someURL") function...
User avatar
Kriek
Forum Contributor
Posts: 238
Joined: Wed May 29, 2002 3:46 am
Location: Florida
Contact:

Post by Kriek »

header() refresh/redirect with delay.

Code: Select all

<?php header("refresh: 5; url=http://$SERVER_NAME/page.php"); ?>
header() refresh/re-direct without delay

Code: Select all

<?php header("Location: http://$SERVER_NAME/page.php"); ?>
Post Reply