Forum Moderators: coopster

Message Too Old, No Replies

php redirect

         

Scally_Ally

9:49 am on Aug 5, 2005 (gmt 0)

10+ Year Member



i have created a form on a simple html page (index.htm) that passes variables to a php page (load_db.php) that loads them into a mysql database.
All works so far but the problem arises when i want to redirect the load_db.php to another after i have loaded the variables to the database.
I have tried using header("Location: index.html"); function but this returns the error
Warning: Cannot modify header information - headers already sent by
I have tried a number of different ways but with no success.
I have done alot of asp programming and this is really easy to do in that (response.redirect), is there a similar function with php?
Thanks
Ally

dreamcatcher

10:06 am on Aug 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,

The problem is you are using the header function after you have already sent data to the browser. For more information see the following:

[uk.php.net...]

You could try using an include, which would load a page or maybe a meta refresh.

Alternatively, try altering your code so that no data gets sent before you call the header function.

dc

Scally_Ally

10:27 am on Aug 5, 2005 (gmt 0)

10+ Year Member



thanks for the quick reply dreamcatcher.

I have already viewed this page before posting, and tried many of the ways it says on it with no success. I have even tried just having

<body><?php header("Location: index.html");?></body>

but this does not work either.
Do you think it is because i am using it on my local machine?
I have thought about using meta tags to refresh the page after a second or so but i feel that this is rather unelegant.
I have seen on a few php pages (especially file upload and php email) that instead of directing to a page to do the processing then directing to another page that the processing is just done at the top of the page that you would be directing to before any html is displayed, thus cutting out a page.
Is this generally how it is done?
Ally

buriedUnderGround

11:40 am on Aug 5, 2005 (gmt 0)

10+ Year Member



There should be NO output before the header() function.

Try putting <? header("Location: index.html");?> at the beginning of your file without the <BODY> tags that you specified.

prometeus

12:19 pm on Aug 5, 2005 (gmt 0)

10+ Year Member



maybe try it in javascript...

<body onload="doRedirect();">

<script language="JavaScript">
<!--
var sTargetURL = "<?=$your_link?>";

function doRedirect() {
// this will load the page in 5 secs...
setTimeout( "timedRedirect()", 5*1000 );
}

function timedRedirect(){
window.location.href = sTargetURL;
}

-->

</script>

Scally_Ally

1:34 pm on Aug 5, 2005 (gmt 0)

10+ Year Member



yeah thanks for that promme..
It works well but it still seems rather unelegant and also some browsers dont support javascript.
thanks again
Ally