Skip to main content
Participating Frequently
August 14, 2009
Answered

Header already sent

  • August 14, 2009
  • 2 replies
  • 1822 views

Hello,

Please excuse me. I know this has been discussed before. I am trying to make a simple user registration form. When the submit botton is pressed I always seem to get

"Warning: Cannot modify header information - headers already sent in C:\xampp\htdocs\cdlist\Untitled-1.php on line 45"

The main code is:

<?php virtual('/cdlist/Connections/cdlist.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }
  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;   
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO users (username) VALUES (%s)",
                       GetSQLValueString($_POST['name'], "text"));
  mysql_select_db($database_cdlist, $cdlist);
  $Result1 = mysql_query($insertSQL, $cdlist) or die(mysql_error());
  $insertGoTo = "yes.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s",$insertGoTo));
}
?>
<!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=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form id="form1" name="form1" method="POST" action="<?php echo $editFormAction; ?>">
  <table width="500" border="1">
    <tr>
      <td>cd name
        <label>
          <input type="text" name="name" id="name" />
          <input type="submit" name="button" id="button" value="Submit" />
      </label></td>
    </tr>
    <tr>
      <td>cd type 
        <label>
          <input type="text" name="type" id="type" />
          <input type="reset" name="button2" id="button2" value="Reset" />
      </label></td>
    </tr>
  </table>
  <input type="hidden" name="MM_insert" value="form1" />
</form>
</body>
</html>

The associated file for the main page cdlist.php is :

<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_cdlist = "localhost";
$database_cdlist = "one_stop";
$username_cdlist = "root";
$password_cdlist = "enterprise";
$cdlist = mysql_pconnect($hostname_cdlist, $username_cdlist, $password_cdlist) or trigger_error(mysql_error(),E_USER_ERROR);
?>

The page that should come up is given by

<!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=utf-8" />
<title>Untitled Document</title>
</head>

<body>
yes it was inserted
</body>
</html>

I really have tried to find the solution and I really don't understand why this doesn't work. Please, any help would be really appreciated.

Thanks

This topic has been closed for replies.
Correct answer David_Powers
<?php include($_SERVER['DOCUMENT_ROOT'].'/cdlist/Connections/cdlist.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }
  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;   
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO users (username) VALUES (%s)",
                       GetSQLValueString($_POST['name'], "text"));
  mysql_select_db($database_cdlist, $cdlist);
  $Result1 = mysql_query($insertSQL, $cdlist) or die(mysql_error());
  $insertGoTo = "yes.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s",$insertGoTo));
}
?>
<!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=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form id="form1" name="form1" method="POST" action="<?php echo $editFormAction; ?>">
  <table width="500" border="1">
    <tr>
      <td>cd name
        <label>
          <input type="text" name="name" id="name" />
          <input type="submit" name="button" id="button" value="Submit" />
      </label></td>
    </tr>
    <tr>
      <td>cd type 
        <label>
          <input type="text" name="type" id="type" />
          <input type="reset" name="button2" id="button2" value="Reset" />
      </label></td>
    </tr>
  </table>
  <input type="hidden" name="MM_insert" value="form1" />
</form>
</body>
</html>

Thanks for the reply David. Here is Untitled-1.php. As you can can see I have edited the first line to the suggested include code.


When I received your reply by email, there was a space between the closing and opening PHP tags here:

<?php include($_SERVER['DOCUMENT_ROOT'].'/cdlist/Connections/cdlist.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {

Any white space outside PHP tags will trigger this problem.

Make sure there is no whitespace at the end of cdlist.php. Make sure there is no whitespace at the end of line 1 above. In fact, it would make more sense to remove the closing tag at the end of line 1 and the beginning one on line 2 like this:

<?php include($_SERVER['DOCUMENT_ROOT'].'/cdlist/Connections/cdlist.php');

if (!function_exists("GetSQLValueString")) {

2 replies

Participating Frequently
August 17, 2009

Reply

Participant
August 15, 2009

I would say that it probably has to do with the virtual() function. According to the php documentation it is better to use include or exclude for php files. From what i can see to include a file using the virtual function it has to go through the web server which is probably causing your "header already sent" plus the virtual() function isn't widely supported. Let me know if the include works better. To accomplish what you are trying with the virtual you can use:

$_SERVER['DOCUMENT_ROOT']

So instead of:

virtual('/test/cdlist.php'); 

use

include($_SERVER['DOCUMENT_ROOT'].'/test/cdlist.php');

That should work for you if not let me know.

Thanks,

Applecorestudios

applecorestudios.com

David_Powers
Inspiring
August 15, 2009

See also this article on headers already sent: http://kb2.adobe.com/community/publishing/505/cpsid_50572.html.