Forum Moderators: coopster

Message Too Old, No Replies

header("location: ERROR

header("location:mypage.php"); ERROR

         

taurus5_6

1:59 am on Dec 4, 2007 (gmt 0)

10+ Year Member



I am having error with header("location:

The error message looks like this

Warning: Cannot modify header information - headers already sent by (output started at /home/kabeet/public_html/flash/page_add.php:4) in /home/kabeet/public_html/flash/page_add.php on line 72

while my codes are the following:

71: if ($resultpage){
72: header("location:detail.php?name_id=$name_id#P$page_id");
73: exit();
74: }else{

What is wrong?

Habtom

3:59 am on Dec 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



taurus5_6, Welcome to WebmasterWorld.

That error shows you have an output somewhere on the top by a different code.

eelixduppy

4:15 am on Dec 4, 2007 (gmt 0)



This could also be whitespace output to the browser before the header call. If you have 70 lines of code before this odds are you are definitely echoing something to the browser before this. A way to get around this is to use output buffers [us2.php.net].

taurus5_6

5:16 am on Dec 4, 2007 (gmt 0)

10+ Year Member



Well I guess I just have to post the entire code here... maybe you can tell me where I am missing. Im a newbie and I don't think I know a lot already.
By the way, the destination page in this case detail.php also uses session cookie.

<?php
session_name('YourVisitID');
session_start();
?>
<html>
<head><title>ADD A PAGE</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="css/global.css" rel="stylesheet" type="text/css">
<style type="text/css">
<!--
.style3 {font-weight: bold}
-->
</style>
</head>
<body bgcolor=#990000><center>
<table width="85%" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
<tr>
<td bgcolor="#FFFFCC"><div align="center"><font size="2" face="Arial, Helvetica, sans-serif">
</font>
<table width="95%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td bgcolor="#FFFFFF"><table width="90%" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" class="main">
<tr>
<td align="center" valign="middle"><div align="center">
<table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
<tr>
<td><a href="../index.php"><img src="../images/top_OLD_r1_c1.gif" alt="mydomain.com Homepage" width="62" height="20" border="0"></a></td>
</tr>
</table>
</div></td>
<td align="center" valign="middle"><a href="http://www.mydomain.com/forums/" target="_blank" class="fields"> </a><a href="http://www.mydomain.com/flash/detail.php?name_id=10205" target="_self" class="fields">NCR MAP</a> <a href="http://www.mydomain.com/forums/" target="_blank" class="fields">FORUMS</a> <a href="http://www.mydomain.com/lists/" target="_self" class="fields">NEWSLETTER</a> <a href="http://www.mydomain.com/news.php" target="_self" class="fields">NEWS</a> <a href="http://www.mydomain.com/contact.php" target="_self" class="fields">CONTACT US</a> <a href="http://www.mydomain.com/flash/result01.php?pid=10183&curPage=1&keyword=hotel&file=millennium.swf&category=Hotel&location=" target="_self" class="fields">HOTELS</a> <a href="http://www.mydomain.com/registration.php?int=A3250" target="_self" class="fields">ADVERTISE</a>
<div align="center"> </div></td>
<td><div align="center" class="style3"></div>
<div align="center"><a href="user_main.php" class="fields">MY PAGE</a></div></td>
<td><div align="center"></div>
<div align="center"><a href="user_logout.php" class="fields">LOGOUT</a></div></td>
</tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td bgcolor="#251200"><img src="../images/blank.gif" width="20" height="3"></td>
</tr>
</table></td>
</tr>
</table>
<font size="2" face="Arial, Helvetica, sans-serif">
<?php
$nxt = $_GET["nxt"];//get next page variable from form
$name_id = $_GET["name_id"]; //get iser id variable from link
$page_id = $_GET["page_id"]; //get iser id variable from link
$name_comp = $_GET["name_comp"]; //get iser id variable from link
if (isset($_POST['submit'])){
$message=NULL;
if(empty($_POST['title'])){
$title=FALSE;
$message.="Please enter the title of this page. ";
}else{
$title=$_POST['title'];
}
if(empty($_POST['content'])){
$content=FALSE;
$message.="Please enter contents of this page.";
}else{
$content=$_POST['content'];
}
if ($title && $content){
include("cn.php");
$querypage="insert into tbl_page (name_id, title, content, dt_created)
values('$name_id','$title','$content',NOW())";
$resultpage=mysql_query($querypage);
if ($resultpage){
header("location:detail.php?name_id=$name_id#P$page_id");
$id=$_SESSION['name_id'];
exit();
}else{
$message='<p>Your page could not be saved because of System Error </p><p>'.mysql_error().'</p>';
}
mysql_close();
}else{
$message .='<strong> Please fill in all the information...</strong>';
}
}
if (isset($message)){
echo'<font color="#3300ff">',$message,'</font>';
}

?>
</font> <font color="#666666" size="2" face="Arial, Helvetica, sans-serif"><strong><BR>
<font color="#0033FF">All Entries are required</font></strong></font>
<form method="post" action="<?php $_SERVER['PHP_SELF'];?>">
<TABLE WIDTH="95%" HEIGHT="50%" BORDER="0" align="center" bgcolor="#FFFFCC">
<TR>
<TD VALIGN=TOP ALIGN="RIGHT">&nbsp;</TD>
<td bordercolor="#FFFFFF" class="eleventextbold"><div align="center">PAGE CONTENT EDITOR </div></td>
<td bordercolor="#FFFFFF" class="redlabelheader">&nbsp;</td>
</TR>
<TR>
<TD VALIGN=TOP ALIGN="RIGHT"><font size="2" face="Arial, Helvetica, sans-serif">&nbsp;</font></TD>
<TD colspan="2"><font size="2" face="Arial, Helvetica, sans-serif"> <strong>TITLE</strong><font color="#FF0000"> (maximum of 30 characters only) </font><br>
<INPUT NAME="title" TYPE="TEXT" id="title" value="<?php if(isset($_POST['title'])) echo $_POST['title'];?>" SIZE="50" MAXLENGTH="30">
<BR>
</font>
</TR>
<TR>
<TD VALIGN=TOP ALIGN="RIGHT">&nbsp;</TD>
<TD colspan="2"><font size="2" face="Arial, Helvetica, sans-serif"> <strong>CONTENTS</strong> <font color="#FF0000"> (html tags are accepted, ex.: &lt;B&gt; <strong>for bold</strong>&lt;/B&gt;, &lt;I&gt; <em>for Italics</em>&lt;/&gt;,</font><font size="2" face="Arial, Helvetica, sans-serif"><font color="#FF0000">&lt;BR&gt; for new line</font></font><font color="#FF0000">) </font><br>
<textarea name="content" cols="100" rows="15" wrap="VIRTUAL" id="content"><?php if(isset($_POST['content'])) echo $_POST['content'];?>
</textarea>
<BR><a href="user_main.php" class="fields">BACK TO MY PAGE</a>
</font>
</TR>
</TABLE>
<font size="2" face="Arial, Helvetica, sans-serif"> </font>
<center>
<p><font size="2" face="Arial, Helvetica, sans-serif">
<input type="submit" name="submit" value="i-KABEET">
<input type="reset" value="i-RESET">
</font></p>
</center>
</form>
<font color="#000000" size="3" face="Arial, Helvetica, sans-serif"></font></div></td>
</tr>
</table>
</body>
</html>

adb64

7:30 am on Dec 4, 2007 (gmt 0)

10+ Year Member



In line 5 you are already sending some HTML to the browser and after that you try to send a header, that can't be done as eelixduppy already mentioned. You have to put the PHP code that emits the header before any HTML.

PHP_Chimp

8:42 am on Dec 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Either use output buffering, ob_start [uk3.php.net] and co, as you can then send headers at any point in your script.
Or you can use this to do it for you -

function redirect($url) {
if (!headers_sent())
header('Location: '.$url);
else {
echo '<script type="text/javascript">';
echo 'window.location.href="'.$url.'";';
echo '</script>';
echo '<noscript>';
echo '<meta http-equiv="refresh" content="0;url='.$url.'" />';
echo '</noscript>';
}
}

Although be warned that this function will not always work, as some antispam attachments will block meta-refresh, and people with javascript turned off will not get redirected either. However this will deal with about 99% of the population.

taurus5_6

1:58 am on Dec 5, 2007 (gmt 0)

10+ Year Member



Thanks for the help guys!

Where do I place the redirect url script? should it be before <html>? or at my header location function?

PHP_Chimp

9:46 am on Dec 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you want to use the function then it can be places almost anywhere.

However you may well want to put all of your php code before all of your html (or after I guess, although I've never tried that). As it makes it easier to see what code is attached to that page. If you have blocks of php scattered throughout the page then it is more difficult for you to track down all of your code.
Sometimes it is unavoidable to have your php split up i.e. if you are breaking out of php to echo large amounts of information, but if you are not then put all of it in 1 place so it is easy to find.

If you have a lot of code then you could always include the php from an external file. So if you page is called page.html you have an included file called page.include.php or something like that, so you know what code goes with what page.

dbarasuk

12:10 am on Dec 13, 2007 (gmt 0)

10+ Year Member



if that error is not yet fixed, try this:

Delete the white space before the opening and ending php tags and the problem will definitely be solved.

If you make an analysis, you'll see that the <?php that begins your php code is preceded by an empty or white space. if It's not the case, this is true for the ending tag.

Delete that (or those spaces) and tell me if it doesn't work