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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Storing pages in mysql database. Getting Header already sent error

Status
Not open for further replies.

sd0t1

IS-IT--Management
Mar 14, 2007
131
US
Hi guys.

I'm storing my web pages in a mysql database and use sql query's to fetch and load the pages. My site effectively consists of 1 page called home.php.
I have query that says
if (isset($_GET['p_name'])){
$getpage = mysql_real_escape_string($_GET['p_name']);
$query = "select * from pages where p_name = '$getpage' ORDER by id DESC limit 1";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result)){
eval('?>' . $row['p_content'] . '<?php ');}}?>



Well my problem is that I use the header() function to redirect users to other pages for numerous reasons like... after a login, after a database insertion etc.
Well each page that is pulled from the database that uses the header() gives me an error that says

Warning: Cannot modify header information - headers already sent by (output started at C:\AppServ\ in C:\AppServ\ : eval()'d code on line 49

Here is a page that pulls from the database (home.php?p_name=logout)

I've been using the meta refresh tag to redirect pages, but it's not as effective and actually shows the page for a split second before if refreshes, even when set on 0 seconds to refresh.


// Unset all of the session variables.
$_SESSION = array();

// Finally, destroy the session.
session_destroy();

echo"<META http-equiv='refresh' content='0;URL=home.php'/>";

I would like to use
header("Location: home.php"); but I get the error.

Any help would be greatly appreciated.
From what I've been reading I think it has something to do with the home page has already sent headers and when I use a query to change the content of the home page and the new content tries to send another header giving me the error. I'm not sure if this is it but maybe.
Also I keep reading to make sure I don't send white spaces before the header, but I don't know what they mean by empty or white spaces being sent causing errors. How do I check? Where do I look for the white spaces?
 
somewhere you are outputting text to the browser before the header line. this can be as simple as having a blank line before your opening php tags, or, more commonly, outputting the doctype at the top of the file before starting your php code.

the output is started at line 94 of home.php
 
In addition to jpadie observation, which is very likely the case, you might want to simply use a JS approach:

<script>window.location.href='home.php';</script>

instead of header() ...


Good luck!
 
Thank you guys so much for the help.
You were both correct (JPadie and Southbeach), I choose to go with the java script it was just as effective and easier.
Here is were I think my problem with the header is.
my page is laid out like this:
<?php
first i hav a php section that sets the session variables
//and gets the connection and calls the includes



if (!isset($_SESSION)) {
session_start();
}
session_register('sid');
session_register('role');
session_register('loggedUser');
$t=time();
$my_t=gettimeofday();
$sid= $_SERVER['REMOTE_ADDR'].("$my_t[sec].$my_t[usec]");
include ("includes/site_security.php");

?>

Then I have my html (and like JPadie said)and I send the doc type

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<html xmlns="<head>
<SCRIPT language="JavaScript">
function submitloginform()
{
document.login.submit();
}
</SCRIPT></head>


then I have the body tag with the site layout i used tables.
Then in the middle of the tables I have my php code that gets and echo's the page content in the proper place.



<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="25">&nbsp;</td>
<td width="94%"><?php
if (isset($_GET['p_name']))
{
$getpage = mysql_real_escape_string($_GET['p_name']);
$query = "select * from pages where p_name = '$getpage' ORDER by id DESC limit 1";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result))
{
eval('?>' . $row['p_content'] . '<?php ');
}
}
?></td>
</tr>
</table>



So I think that I would have to switch my code to run the script before the doc type is sent and load the page content into a variable then echo the variable into the table in the center of the page.
 
Well, I do not see where you decide to switch page or URL but I figure it is code read from your SQL table. That said, if you are loading PHP code from SQL and expect it to execute, you CANNOT use a variable and simply echo the variable.

I would load the content, save it to a temporary file then include that file. Once file is included, you can then remove it. Heck, you can load the processed page to a variable then echo the variable.

As it often is the case in our field, you will find that there are many ways to get things done. Hope you are now in the path to solving your problem. Post here any other question you may have!

Good luck!




 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top