Jump to content

Cannot modify header information - headers already sent by...


etymole

Recommended Posts

Somebody please help me...I'm relatively new to PHP...

I've been working from the visual quickstart guide to php and mysql and have been having trouble with sessions when trying to login...I've googled my errors but still get the same problem, or just have my script not work properly.

 

 

The error I'm getting is - Warning: Cannot modify header information - headers already sent by (output started at /home/content/g/k/o/gkodikara/html/ecomm/mysql_connect.php:10) in /home/content/g/k/o/gkodikara/html/ecomm/ecommwebsite/checklogin.php on line 33

 

I'm just not sure what this means. I have moved the session_start() and header() functions about but this has had little effect and makes my script stop working completely...

 

I will post the code I am using including the mysql_connect script mentioned in the error -

 

login.php

<?php # Script 9.15 - login.php (7th version after Scripts 9.1, 9.3, 9.6, 9.10. 9.13 & 9.14)
// Send NOTHING to the Web browser prior to the session_start() line!
session_start();
// Create a function for escaping the data.
function escape_data ($data) {
	global $dbc; // Need the connection.
	if (ini_get('magic_quotes_gpc')) {
		$data = stripslashes($data);
	}
	return mysql_real_escape_string(trim($data), $dbc);
} // End of function.

// Check if the form has been submitted.
if (isset($_POST['submitted'])) {

require_once ('mysql_connect.php'); // Connect to the db.

$errors = array(); // Initialize error array.

// Check for an email address.
if (empty($_POST['email'])) {
	$errors[] = 'You forgot to enter your email address.';
} else {
	$e = escape_data($_POST['email']);
}

// Check for a password.
if (empty($_POST['password'])) {
	$errors[] = 'You forgot to enter your password.';
} else {
	$p = escape_data($_POST['password']);
}

if (empty($errors)) { // If everything's OK.

	/* Retrieve the user_id and first_name for 
	that email/password combination. */
	$query = "SELECT user_id, first_name FROM users WHERE email='$e' AND password=SHA('$p')";		
	$result = @mysql_query ($query); // Run the query.
	$row = mysql_fetch_array ($result, MYSQL_NUM); // Return a record, if applicable.

	if ($row) { // A record was pulled from the database.

		// Set the session data & redirect.
		session_name ('YourVisitID');

		$_SESSION['user_id'] = $row[0];
		$_SESSION['first_name'] = $row[1];
		$_SESSION['agent'] = md5($_SERVER['HTTP_USER_AGENT']);

		// Redirect the user to the loggedin.php page.
		// Start defining the URL.
		$url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
		// Check for a trailing slash.
		if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
			$url = substr ($url, 0, -1); // Chop off the slash.
		}
		// Add the page.
		$url = '/loggedin.php';

		header("Location: $url");
		exit(); // Quit the script.

	} else { // No record matched the query.
		$errors[] = 'The email address and password entered do not match those on file.'; // Public message.
		$errors[] = mysql_error() . '<br /><br />Query: ' . $query; // Debugging message.
	}

} // End of if (empty($errors)) IF.

mysql_close(); // Close the database connection.

} else { // Form has not been submitted.

$errors = NULL;

} // End of the main Submit conditional.

// Begin the page now.
$page_title = 'Login';


if (!empty($errors)) { // Print any error messages.
echo '<h1 id="mainhead">Error!</h1>
<p class="error">The following error(s) occurred:<br />';
foreach ($errors as $msg) { // Print each error.
	echo " - $msg<br />\n";
}
echo '</p><p>Please try again.</p>';
}
?>
<!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>
         
<title>Clubdate.net - Dating for club lovers!</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
  <div id="wrapper">
      <h1><a href=""><img src="images/logo.gif" width="554" height="47" alt="ClubDate.net" /></a></h1>
      <div id="booking">
        <h2>Member Login</h2>
         

         
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" >
<tr>
<form name="form1" method="post" action="checklogin.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1">
<tr>
<td colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="email" type="text" id="email"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="password" type="text" id="password"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>

                            
                     
                 
      </div>
      <!-- end search -->
      <div id="nav">
        <ul>
          <li><a href="index.php">Home</a></li>
          <li><a href="about.php">About Us</a></li>
          <li><a href="testimonials.php">Testimonials</a></li>
          <li><a href="login.php">Register/Login</a></li>
          <li><a href="support.php">Support</a></li>
        </ul>
      </div><!-- end nav -->
      <h2  id="packagesheader"><img src="images/title_our_packages.gif" width="352" height="23" alt="our packages" /></h2>
      <div id="packages">
<?php
// Create a login/logout link.
if ( (isset($_COOKIE['user_id'])) && (!strpos($_SERVER['PHP_SELF'], 'logout.php')) ) {
echo '<a href="logout.php" title="Logout">Logout</a>';
} else {
echo '<a href="login.php" title="Login">Login</a>';
}
?>



          <div id="special">
              <a href="advertising.php"><img src="images/ad_special_offer.gif" width="293" height="79" alt="Special Offer!" /></a>
          </div><!-- end special -->
          
      </div><!-- end packages -->
      <div id="main">
            <img src="images/people.jpg" width="447" height="298" alt="Two people having a drink at a bar" class="block" />
            <h2><img src="images/title_featured_members.gif" width="447" height="24" alt="featured members" /></h2>
            
            <!-- I will make the following section dynamic when I create the SQL and PHP end of the websites -->
          <div class="inner">
            <h3 class="blue">Larissa</h3>
            <img src="images/photo_1.jpg" width="109" height="71" alt="stunning italian history" class="left" />
             <p>Larissa is a 23 year old girl from Romania! <br />
             Favorite clubs include: Pacha, Vendome and Jacuzzi!</p>

			<br /><br />
            <h3 class="green">Greg</h3>
            <img src="images/photo_2.jpg" width="109" height="71" alt="sea, the beaches" class="left" />
		<p>Greg is a 24 year old guy from London! <br />
            Favorite clubs include: K.O, Area and Kandi Club!</p>
          
            <div class="clear"></div>
          </div>
          <div class="clear"></div>
      </div><!-- end main -->
      <div id="footer">
     <center> © Copyright ClubDate.net&#8482; 2010 | <a href="contact.php">Contact</a> | <a href="login.php">Member Login</a>  </center>
      </div><!-- end footer -->
  </div><!-- end wrapper -->
</body>
</html>

 

mysql_connect.php

<?php #mysql_connect

DEFINE ('DB_USER', '------');
DEFINE ('DB_PASSWORD', '-----');
DEFINE ('DB_HOST', '-------');
DEFINE ('DB_NAME', '-------');

//Make the connection

$dbc = @mysql_connect (DB_HOST, DB_USER, 
DB_PASSWORD) OR die ('Could not connect to MySQL: ' . mysql_error() );

//Select the database
@mysql_select_db (DB_NAME) OR die ('Could not Select Database: ' . mysql_error() );
?>

 

Obviously I have taken out my login details...

 

Any help would be greatly appreciated...

Link to comment
Share on other sites

that error is from your header() call. you can't output any information (likely html) to the browser before this is called.

 

there is a space or something being outputted in your mysql_connect.php file. make sure you remove any spaces before and after the <?php ?> tags.

Link to comment
Share on other sites

Check for white space either before the <?php or after the ?>  Usually. "cannot modify header information, already sent by whatever:0" means you have a space or a new line either before the opening tag or after the closing tag.

 

Make sure the first thing that PHP sees when in runs an include is the opening tag, even if you have to throw a <?php echo "";?> on the top just to be sure.  NO spaces or lines before or after your PHP tags on any page used as, or which uses an include, is what I im finding... :)

 

 

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.