Jump to content

[SOLVED] Problem With require() and header()


centerwork

Recommended Posts

Here is my problem. My pages use the script below to connect to the database:

 

<?php
//Establishes A Connection With The Database
require ($_SERVER["DOCUMENT_ROOT"] . "/dir/ssl/config.php");
$connection = mysql_connect($db_host, $db_user, $db_password, $db_name) or die ("error connecting");
mysql_select_db("my_db"); 
?>

 

When I run any of my pages with a:

 

header("Location: http://www.mysite.com/dir/somepage.php");

 

I get this error below.

 

Warning: Cannot modify header information - headers already sent by (output started at /some_dir/some_file.php:11)

 

I know this error is caused by having the header() called after something is echo/print.

 

My file config.php has no header. Here is the Complete File.

 

<?php
$db_host = "host server";
$db_user = "username";
$db_password = "password";
$db_name = "my_db";
?>  

   

 

I have figured that the server is echoing out the the html, header, body on its own.

 

What can I do to just have it load the variables?

Link to comment
Share on other sites

output started at /some_dir/some_file.php:11

 

Start by reading the error message. Whatever the actual file name is, line 11 is where the output was started and since neither of the pieces of posted code has 11 lines, you would need to post the first 12 or so lines of the file that the error message is referring to.

Link to comment
Share on other sites

My file config.php has no header. Here is the Complete File.

 

<?php
$db_host = "host server";
$db_user = "username";
$db_password = "password";
$db_name = "my_db";
?>  

   

 

This is the complete file.

 

The error stats that this file has already sent a header. See error below.

 

Warning: Cannot modify header information - headers already sent by (output started at /some_dir/some_file.php:11)

 

As far as the ":11" If that means lines then it must have the html, header, and body echoed out.

Link to comment
Share on other sites

That is config.php.  The error is in some_file.php

 

My file config.php has no header. Here is the Complete File.

 

<?php
$db_host = "host server";
$db_user = "username";
$db_password = "password";
$db_name = "my_db";
?>  

   

 

This is the complete file.

 

The error stats that this file has already sent a header. See error below.

 

Warning: Cannot modify header information - headers already sent by (output started at /some_dir/some_file.php:11)

 

As far as the ":11" If that means lines then it must have the html, header, and body echoed out.

Link to comment
Share on other sites

Here is the full error.

 

Warning: Cannot modify header information - headers already sent by (output started at /home/content/c/e/n/centerworkpro/html/hme/ssl/db_config.php:11) in /home/content/c/e/n/centerworkpro/html/hme/cust_admin/index.php on line 23

 

Warning: Cannot modify header information - headers already sent by (output started at /home/content/c/e/n/centerworkpro/html/hme/ssl/db_config.php:11) in /home/content/c/e/n/centerworkpro/html/hme/cust_admin/index.php on line 102

 

Warning: Cannot modify header information - headers already sent by (output started at /home/content/c/e/n/centerworkpro/html/hme/ssl/db_config.php:11) in /home/content/c/e/n/centerworkpro/html/hme/cust_admin/index.php on line 103

 

Warning: Cannot modify header information - headers already sent by (output started at /home/content/c/e/n/centerworkpro/html/hme/ssl/db_config.php:11) in /home/content/c/e/n/centerworkpro/html/hme/cust_admin/index.php on line 117

 

This is the complete db_config.php file.

 

<?php
$db_host = "host server";
$db_user = "username";
$db_password = "password";
$db_name = "my_db";
?>

 

Here is the File were the error is generated.

<?php 
//Establishes A Connection With The Database
require ($_SERVER["DOCUMENT_ROOT"] . "/hme/ssl/db_config.php");
$connection = mysql_connect($db_host, $db_user, $db_password, $db_name) or die ("error connecting");
mysql_select_db("centerwork_db"); 

//Checks if there is a login cookie
$username = $_COOKIE['ID_my_site']; 
$pass = $_COOKIE['KEY_my_site'];

$query = "SELECT * FROM cust_login WHERE user = '$username'";
$results = mysql_query($query)or die(mysql_error());
$rows = mysql_fetch_array($results);
$numrows =mysql_num_rows($results);

$db_user = $rows['d_cont_email'];
$db_pass = $rows['b_cont_password'];
$db_pass_default = $rows['pass_default'];
$date=time();

if ($pass == $db_pass) 
{
header("Location: http://www.centerworkpro.com/hme/cust_admin/membersarea.php");
}

// if form has been submitted
if(strlen($_GET['cr'])>0)
{
$change_result = $_GET['cr'];
$username = urldecode($_GET['u']);
}

$submit = $_POST['submit'];
$remember_me = $_POST['remember_me'];
$user = $_POST['username'];
$pass = $_POST['pass'];
$date=time();
$login="yes";

//if the login form is submitted

if (strlen($submit)>0) 
{ 
// if form has been submitted
// makes sure they filled it in
if(!$user | !$pass)
	{
	$e_feilds = "no";
	$login="no";
	}

$query = "SELECT * FROM cust_login WHERE user = '$user'";
$results = mysql_query($query)or die(mysql_error());
$rows = mysql_fetch_array($results);
$check2 = mysql_num_rows($results);

$db_user = $rows['user'];
$db_pass = $rows['pass'];
$db_pass_default = $rows['pass_default'];

//Gives error if user dosen't exist
if ($check2 == 0) 
	{
	$e_user = "no";
	$login="no";
	}

//gives error if the password is wrong
if($db_pass_default != 0)
	{
	$pass = md5($pass);
	if ($pass != $db_pass) 
		{
		$e_pass = "no";
		$login="no";
		}
	}
else
	{
	if ($pass != $db_pass) 
		{
		$e_pass = "no";
		$login="no";
		}
	}

if($login == 'yes')
	{
	// if login is ok then we add a cookie 
	if ($remember_me==yes)
		{
		$hour = time()+60*60*24*30; 
		}
	else
		{
		$hour = time()+60*60;
		}	
	//Saves information for site.
	$username = addslashes($user);
	$pass = addslashes($pass);

	setcookie(ID_my_site, $username, $hour);
	setcookie(KEY_my_site, $pass, $hour);

	//then redirect them to the members area
	if ($db_pass_default == 0)
		{
		$vcode = '';  
		for ($i = 0; $i < 16; $i++) 
			{      
			$vcode .= chr(mt_rand(65,90));  
			}
		$query = "UPDATE cust_login SET cust_login.vcode = '$vcode' WHERE user = '$username'";
		$results = mysql_query($query)or die(mysql_error());
		$username = urlencode($username);
		$vcode = urlencode($vcode);
		header("Location: http://www.centerworkpro.com/hme/cust_admin/passwordchange.php?u=".$username."&v=".$vcode."");
		}
	else
		{
		header("Location: http://www.centerworkpro.com/hme/cust_admin/membersarea.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=iso-8859-1" />
<meta name="robots" content="noindex, nofollow">
<title>HME Customer Login</title>
<style type="text/css">
<!--
body {
background-color: #DDDFFF;
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}
-->
</style></head>

<body>
<div id="content" align="center">
<center>
<table width="354" border="0" cellpadding="10" cellspacing="5">
<tr>
<td width="1%" align="center" valign="middle">
<img src="../images/hme_logo_tran.png" alt="Logo" /></td>
</tr>
<tr>
<td width="169" align="center" valign="middle">
<strong style="font-size:x-large; font-weight:bolder;">Customer Login</strong>
</td>
</tr>
</table>
<br />
<?php
//Prints Errors if they are active.
if (strlen($submit)>0) 
{ 
if($e_feilds == "no")
	{
	echo '<span style="color:#FF0000; font:large; font-weight:bold;">**** You did not fill in a required field. **** </span><br /><br />';
	}
if ($e_user == "no") 
	{
	echo '<span style="color:#FF0000; font:large; font-weight:bold;">**** That user does not exist in our database. **** </span><br /><br />';
	}
if($e_pass == "no")
	{
	echo '<span style="color:#FF0000; font:large; font-weight:bold;"> **** Incorrect password, please try again. **** </span><br /><br />';
	}
}

//If password has been change then this message will display.
if($change_result == '1')
{
echo '<span style="color:#000000; font:large; font-weight:bold;"> **** Your New password has been updated, You must now login. **** </span><br /><br />';
}

?>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<table width="300" border="1" cellpadding="10" cellspacing="5" bordercolor="#000000" bgcolor="#AEB5FF">
<tr>
<td>
<table border="0" cellpadding="0" bgcolor="#AEB5FF">
<tr>
	<td><strong>Username/Email:</strong></td>
	<td><input type="text" name="username" maxlength="64" value="<?php if(strlen($username)>0) { echo $username; } ?>"></td>
</tr>
<tr>
	<td><strong>Password:</strong></td>
	<td><input type="password" name="pass" maxlength="64"></td>
</tr>
</table>
</td>
</tr>
<tr bgcolor="#355B95">
<td colspan="2"><div style="float:left;"><input type="checkbox" name="remember_me" value="yes" /><strong>Remember me next time.</strong></div><div style="float:right;"><input type="submit" name="submit" value="Login"></div></td>
</tr>
</table>
<br />
<table width="300" border="1" cellpadding="5" cellspacing="5" bordercolor="#000000" bgcolor="#FFFF66">
<tr align="center">
<td valign="middle">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr align="center">
	<td align="center" valign="middle">
	<img src="../images/notation.png" alt="Notation" width="16" height="16" />
	</td>
	<td align="center" valign="middle">
	<strong>Cookies must be enabled to continue.</strong>
	</td>
</tr>
</table>
</td>
</tr>
</table>
<br />
<table width="300" border="1" cellpadding="10" cellspacing="5" bordercolor="#000000" bgcolor="#AEB5FF">
<tr align="center">
<td><div id="links" style="float:none; margin:0;"><strong>Forget your password?</strong>  <a href='../biz_admin/password_reset.php' tabindex='1' title='Registration Link'  style="color:#333333;"><strong>Click here.</strong></a></div></td>
</tr>
</table>
</form>
</div>
</body>
</html>

 

As far as I can tell the there is no header() after the <head> tag.

 

This only leaves the db_config.php file.

 

A version of this file was used on yahoo hosting with no problem. It is now hosted on godaddy. I'm not sure if that helps at all.

 

Link to comment
Share on other sites

My guess would be that you have 11 or so newlines before the opening <?php tag or after the closing ?> tag.

This is most likely correct.

 

The "complete" db_config.php you posted is only 6 lines long, yet the error is referencing line 11 of db_config.php.

 

Are you sure you have absolutely no spaces or newlines before the <?php or after the ?> in db_config.php?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.