Jump to content

insane header problem.


sniperscope

Recommended Posts

Hi gurus!!

My homepage was working well untill 2 hours ago and all of sudden i had this error.

Warning: Cannot modify header information - headers already sent by (output started at /home1/istanbu4/public_html/mysite/header.php:13) in /home1/istanbu4/public_html/mysite/write.php on line 2

 

Unfortunately my site is working on localhost and was working on server. I have no any clue why this error occurred.

Here is my connection file(Cn.php)

<?php
$hostname = "localhost";
$dbase = "istanbu4_sitem";
$uname = "USER_NAME";
$pword = "PASS";
$cn_ = mysql_pconnect($hostname, $uname, $pword) or trigger_error(mysql_error(),E_USER_ERROR);
mysql_select_db($dbase, $cn_) or die("Could not Connect Database Please Try again later !!!");
?>

 

And this is my header file(header.php)

<?php
if(!isset($_SESSION['name'])){
	session_start();
}
require_once('Cn/cn.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=shift_jis" />
<title></title>
<link href="Css/style.css" rel="stylesheet" type="text/css" />
<script language="javascript" src="Jscript/RollOverImage.js" type="text/javascript"></script>
<script language="javascript" src="Jscript/CheckUncheck.js" type="text/javascript"></script>
</head>

<body onload="MM_preloadImages('img/body_on.gif','img/family_on.gif','img/living_on.gif','img/move_on.gif','img/object_on.gif','img/work_on.gif')">
<div id="header"></div>

 

An this is my write.php file

<?php include("header.php"); ?>
<?php if(!isset($_SESSION['name'])) header('location: register.php');

if(isset($_POST['id']) && $_POST['id'] == 1){
	$title	= stripslashes($_POST['ConfTitle']);
	$body	= stripslashes($_POST['ConfBody']);
	$user	= $_SESSION['name'];
	$date	= date("Y/m/d");
	$flg	= 0;

	if(!isset($_POST['ChkBox'])){
		header("location: ". $_SERVER["PHP_SELF"]."?r=2");
		exit;
	} else if($title == "" || $body == ""){
		header("location: ". $_SERVER["PHP_SELF"]."?r=0");
	} else{
		$MysqlQuery = mysql_query("INSERT INTO comment (title, comment, user, c_date, valid)VALUES('$title', '$body', '$user', '$date', '$flg')", $cn_) or die(mysql_error());
	 	header("location: ". $_SERVER["PHP_SELF"]."?r=1");
	}
}
?>
<div id="NavigateMenu">  
	<a href="index.php" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('image1','','img/body_on.gif',0)">
		<img src="img/body_off.gif" name="image1" border="0" id="image1" />
	</a>
	<a href="write.php" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('image2','','img/family_on.gif',0)">
		<img src="img/family_on.gif" name="image2" border="0" id="image2" />
	</a>
	<a href="search.php" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('image3','','img/work_on.gif',0)">
		<img src="img/work_off.gif" name="image3" border="0" id="image3" />
	</a>
	<a href="register.php" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('image4','','img/living_on.gif',0)">
		<img src="img/living_off.gif" name="image4" border="0" id="image4" />
	</a>
	<a href="rules.php" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('image5','','img/object_on.gif',0)">
		<img src="img/object_off.gif" name="image5" border="0" id="image5" />
	</a>
	<a href="contact.php" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('image6','','img/move_on.gif',0)">
		<img src="img/move_off.gif" name="image6" border="0" id="image6" />
	</a>
</div>
<div id="MainBody">
	<?php include('col_left.php'); ?>
	<div id="MainBlock">

I am hopeless, i tried every php code which i know but problem is still out there and i dont know what i have to do.

 

Any help will be great appreciate.

 

Regards

Link to comment
https://forums.phpfreaks.com/topic/160796-insane-header-problem/
Share on other sites

Thanks for reply,

So due to your comment i have to move session_start()(mean whole php block) part into Cn.php file and rest of html code put into another file and include after SQL query ? Is that true ?

Not necessarily. You could output buffer the html, so that it isn't sent to the browser until you actively send the output buffer at the very end of your script. That way, any headers in the middle of the code get sent before the output, even if the output is "echoed" beforehand

Ok. i just changed my code as in my previous post but in that time, i had login problem and it was impossible to login site even if i type correct username adn password.

But after that, i took off whole html code from the header.php file and put every single pages in my site. So far, there is no problem. I can login and read my messages ...etc. But i want to know that why my code was worked well but stop all of sudden ? and why i was unable to login change the code(actually i did not remove and/or insert any extra code) and i never use mysql_close() command after my codes. is that makes any problem? if yes, is that problem related to my current situation?

Thanks for help and replies

 

Regards

Ok. i just changed my code as in my previous post but in that time, i had login problem and it was impossible to login site even if i type correct username adn password.

But after that, i took off whole html code from the header.php file and put every single pages in my site. So far, there is no problem. I can login and read my messages ...etc. But i want to know that why my code was worked well but stop all of sudden ? and why i was unable to login change the code(actually i did not remove and/or insert any extra code) and i never use mysql_close() command after my codes. is that makes any problem? if yes, is that problem related to my current situation?

Thanks for help and replies

 

Regards

 

this is the line causing you errors:

 

<?php if(!isset($_SESSION['name'])) header('location: register.php');

 

it's in write.php, and it's directly after you include header.php. header() is triggering the error, and it's because it is being called after including header.php, which outputs HTML to the browser. this much you know.

 

the reason why it may have suddenly changed is that you'll only see this error if $_SESSION['name'] is not set. presumably you've always been accessing this page AFTER logging in, and therefore the session variable is set. however, if you were to try to access it without having logged in first, it would trigger the error.

 

the best way around this is to check whether the user is logged in in header.php, rather than write.php:

 

<?php
   // might as well start the session no matter what, because without starting the session, no session vars will be set
   session_start();
   // now, if name isn't set, boot them to registration
   if(!isset($_SESSION['name'])){
      header('Location: register.php');
   }
   require_once('Cn/cn.php');
?>

 

if you use header.php on ALL pages (even the unprotected ones), then you can simply set a variable such as $force_login and check for that at the same time as checking whether the user is logged in:

 

   if(isset($force_login) && !isset($_SESSION['name'])){

 

write.php should then start like this:

 

<?php
// tell header.php to force the user to be logged in
$force_login = TRUE;
// include the header file
include("header.php");
?>

 

there's no reason you should ever have to use header() after browser output - having to do so is generally bad code design, because usually whether or not you need to use of header() can be determined before sending output to the browser.

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.