Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP101: redirect to another page

Status
Not open for further replies.

DaveC426913

Programmer
Jul 28, 2003
274
CA
PHP 101:
What is the appropriate way to directly go to another page? (Yes, I really do mean just that.)

I am currently using this JavaScript kludge:

?>
<script>location="nextpage.html"</script>
<?


I read something about "header", but it did not work for me.
 
Yes, I have tried this. I get this error:

Warning: Cannot modify header information - headers already sent by (output started at D:\hshome\davec426\davesbrain.ca\aura\test.php:7) in D:\hshome\davec426\davesbrain.ca\aura\test.php on line 45
 
Sooooo...

What that's telling me is that I should make sur I'm doing all my redirecting (and thus all my PHP code) inside the <head> tag. I can't have it inside the <body> tag.

Correct? (I mean, I know there are other ways to deal with this, but the above way will solve the problem, right?)
 
No.

As the FAQ explains in section 1.4, all headers have to be sent before any content is sent. The order of operations in an HTTP response is "send headers", "send a blank line", "send content". Your script is trying to "send headers", "send a blank line", "send some content", "send another header".

Your script must set headers before it does anything else.



Want the best answers? Ask the best questions! TANSTAAFL!
 
How can I "set headers before I do anything else" if there's stuff I need to do on this page before I redirect?

1] Parse querystring.
2a] If querystring contains 'Command=Save' then save all form data from querystring, then redirect to homepage.
2b] If querystring contains 'Command=Continue' then save all form data, then redirect to page 2 using a form to pass some variables.


Thus:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
<title>Write Answers</title>
<?php
$section = $_POST['section'];
$UID = $_POST['UID'];
$SaveExit = $_POST['SaveContinue'];

#message data, open database connection
#...
$sql = "INSERT INTO `Aura_User_Answer` (UserID, QuestionID,Answer) VALUES (".$UID.",".$QuID.",".$Ans.")";
$result = mysql_query($sql);
$result = mysql_query($sql);
mysql_close($dbcnx);
# close
if ($SaveExit=="Save"){
# Save/Exit flag was set to Save
header("Location: index.html");
}
else{
# Save/Exit flag was set to Continue
?>
</head>
</body>
<!-- hereafter, build a form so you submit form data for POSTing -->



So, regardless of what happens later in the page, it chokes on the 'header' line.
 
I'm sorry, I should have been more clear.

Set the header before you send any more data to the client. Before any print statements. Before any bare HTML outside of <?php...?> tags.



Want the best answers? Ask the best questions! TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top