Jump to content

Header error... found solution, but curious if proper, Please help.


blesseld

Recommended Posts

Hey All,

 

Quick question,  I was following a book, and im just getting the example setup.  I understand what's going on, but I had a header issue ONLY on logout.  So I looked through my functions and header file.

here's the header error:

 

Warning: Cannot modify header information - headers already sent by (output started at /home/tbaynigh/public_html/members/tbnl-header.php:10) in /home/tbaynigh/public_html/members/tbnl-functions.php on line 35
You have been logged out. Please click here to refresh the screen.

 

So it's still working just got the error, then i remembered about ob_start();

 

Here is my header

<?php
include 'tbnl-functions.php';
session_start();
if (isset($_SESSION['user']))
{
$user = $_SESSION['user'];
$loggedin = TRUE;
}
else $loggedin = FALSE;
echo "<html><head><title>$appname";
if ($loggedin) echo " ($user)";
echo "</title></head><body><font face='verdana' size='2'>";
echo "<h2>$appname</h2>";
if ($loggedin)
{
echo "<b>$user</b>:
<a href='tbnl-members.php?view=$user'>Home</a> |
<a href='tbnl-members.php'>Members</a> |
<a href='tbnl-friends.php'>Friends</a> |
<a href='tbnl-messages.php'>Messages</a> |
<a href='tbnl-profile.php'>Profile</a> |
<a href='tbnl-logout.php'>Log out</a>";
}
else
{
echo "<a href='index.php'>Home</a> |
<a href='tbnl-signup.php'>Sign up</a> |
<a href='tbnl-login.php'>Log in</a>";
}
?>

 

Here are my functions:

<?php
$dbhost = 'localhost'; // Unlikely to require changing
$dbname = 'dbname'; // Modify these...
$dbuser = 'user'; // ...variables according
$dbpass = 'pass'; // ...to your installation
$appname = "Business Profiles"; // ...and preference
mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());
function createTable($name, $query)
{
if (tableExists($name))
{
echo "Table '$name' already exists<br />";
}
else
{
queryMysql("CREATE TABLE $name($query)");
echo "Table '$name' created<br />";
}
}
function tableExists($name)
{
$result = queryMysql("SHOW TABLES LIKE '$name'");
return mysql_num_rows($result);
}
function queryMysql($query)
{
$result = mysql_query($query) or die(mysql_error());
return $result;
}
function destroySession()
{
$_SESSION=array();
if (session_id() != "" || isset($_COOKIE[session_name()]))
setcookie(session_name(), '', time()-2592000, '/');
session_destroy();
}
function sanitizeString($var)
{
$var = strip_tags($var);
$var = htmlentities($var);
$var = stripslashes($var);
return mysql_real_escape_string($var);
}
function showProfile($user)
{
if (file_exists("$user.jpg"))
echo "<img src='$user.jpg' border='1' align='left' />";
$result = queryMysql("SELECT * FROM tbnlprofiles WHERE user='$user'");
if (mysql_num_rows($result))
{
$row = mysql_fetch_row($result);
echo stripslashes($row[1]) . "<br clear=left /><br />";
}
}
?>

 

 

After Adding ob_start();  after  session_start(); in my header, I was able to logout without errors.

 

My quesiton is, Is this a proper thing to do?  I am not familiar enough with PHP and don't want to run into issues down the road.

 

Yes, the output buffer (ob) is the only method to send a header after the page has been outputted. But note you have to place ob_start() at the TOP of your script before anything is outputted. Your sessions does not, but it is better practise or you will end up with problems along the road..

<?php
ob_start(); //Start output buffer
include 'tbnl-functions.php';
session_start();

ob_start

Awesome, 

 

And what about ob_end_flush()

 

Should that be at the bottom of my logout script?  or do I not need to flush unless using multiple ob_start()?

 

If you're using header() than it will terminate the script, there is nothing more to be outputted. So there is no need to use ob_end_flush unless you require the output buffer to do something other than header();.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.