Hey DevNetwork!!!
It's my first day on the site, I just registered. I registered because this site seems like a cool place and I need help with php.
I just started learning php but I know many other languages.
I'm trying to create a login so the user can access the admin panel but I've come accross this "Cannot modify header information" problem. I've researched the problem a bit on the net and everyone says it's the white space the problem but there is no more white space after and before the "<?" and "?>".
Here is the complete error:
Warning: Cannot modify header information - headers already sent by (output started at c:\inetpub\wwwroot\crl\header.php:7) in c:\inetpub\wwwroot\crl\login.php on line 68
I have a header template named header.php and then the login page login.php.
Here is the content of "header.php":
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Clarence-Rockland Girls Hockey Association</title>
<meta name="DESCRIPTION" content="Clarence-Rockland Girls Hockey Association">
<meta name="KEYWORDS" content="hockey, clarence, rockland, hockey association, girls hockey, clarence-rockland girls hockey association">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<table cellspacing="0" cellpadding="0" width="750" align="center" class="maintable">
<tr>
<td><img src="images/banner_left.jpg" alt="Clarence-Rockland Girls Hockey Association"></td>
<td><img src="images/banner_right.jpg" alt="Clarence-Rockland Girls Hockey Association"></td>
</tr>
<tr>
<td valign="top">
<div class="navigation">
<ul class="navlist">
<li><a href="index.php">Invitation Cover Page</a></li>
<li><a href="contactlist.php">Committee Contact Info</a></li>
</ul>
</div>
<img src="images/nav_bottom.jpg" alt="">
</td>
<td colspan="2" class="content">
Here is the content of "login.php":
<?php include('header.php'); ?>
<?php ob_start(); ?>
<h1>Admin Login</h1>
<?php
function escape_data($data){
global $dbc;
if(ini_get('magic_quotes_gpc')){
$data = stripslashes($data);
}
return mysql_real_escape_string(trim($data), $dbc);
}
//Check if the form has been submitted
if(isset($_POST['submitted'])){
//Connect to database
require_once('mysql_connect.php');
//Validate Username
if(!empty($_POST['username'])){
$u = escape_data($_POST['username']);
} else {
echo '<p style="color:red;">Username is required.</p>';
$u = false;
}
//Validate Password
if(!empty($_POST['pword'])){
$p = escape_data($_POST['pword']);
} else {
echo '<p style="color:red;">Password is required.</p>';
$p = false;
}
//If everything is OK
if($u && $p){
//Query the database
$query = "SELECT userid, username, pword FROM users WHERE (username='$u' AND pword='$p')";
$result = mysql_query($query) or trigger_error("Query: $query\n<br> MySQL Error: " . mysql_error());
//A match was made
if(@mysql_num_rows($result) == 1){
//Register the values and redirect
$row = mysql_fetch_array($result, MYSQL_NUM);
mysql_free_result($result);
//Close the database connection
mysql_close();
$_SESSION['username'] = $row[1];
$_SESSION['userid'] = $row[0];
//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) == '\\')){
//Chop off slash
$url = substr($url, 0, -1);
}
//Add the page
$url .= '/news.php';
//Delete the buffer
ob_end_clean();
header("Location: $url");
//Quit the script
exit();
} else {
echo '<p style="color:red;">Please try again.</p>';
}
mysql_close();
}
}
?>
<form name="loginform" method="post" action="login.php">
<table cellspacing="2" cellpadding="0" align="center" class="login">
<tr>
<th>Username</th>
<td><input type="text" name="username" class="textfield"></td>
</tr>
<tr>
<th>Password</th>
<td><input type="password" name="pword" class="textfield"></td>
</tr>
<tr>
<td colspan="2" style="text-align:right;">
<input type="submit" name="submit" value="Login" class="button">
<input type="hidden" name="submitted" value="TRUE">
</td>
</tr>
</table>
</form>
<?php include('footer.php'); ?>
I tried replacing header("Location: $url"); by echo "<script> self.location(\"news.php\");</script>"; and still didn't work.
Can anyone help???
Chris
Cannot modify header information
Moderator: General Moderators
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
The header that you're including echo's content back to the browser so you can't send HTTP headers using PHP.
I can see you're tring to use ob_ to work around this... the problem is that you've put ob_start() after you included the first file.... move it to before the include(); and hopefully that will help (at first glance)
I can see you're tring to use ob_ to work around this... the problem is that you've put ob_start() after you included the first file.... move it to before the include(); and hopefully that will help (at first glance)
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
os_start() = band-aid. Fix the problem, don't hide it.
And please, post code in the appropriate tags (
And please, post code in the appropriate tags (
Code: Select all
orCode: Select all
) when you post code.