Jump to content

Warning: Cannot modify header information - headers already sent in


bulgaria_mitko

Recommended Posts

can u help me I'm getting a error in the begging of the page it is loading correctly except for this warning at the very begging this is my php code:

[code]<?php
if (!isset($_SESSION)) {
  session_start();
}
$MM_authorizedUsers = "1,2";
$MM_donotCheckaccess = "false";

// *** Restrict Access To Page: Grant or deny access to this page
function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) {
  // For security, start by assuming the visitor is NOT authorized.
  $isValid = False;

  // When a visitor has logged into this site, the Session variable MM_Username set equal to their username.
  // Therefore, we know that a user is NOT logged in if that Session variable is blank.
  if (!empty($UserName)) {
    // Besides being logged in, you may restrict access to only certain users based on an ID established when they login.
    // Parse the strings into arrays.
    $arrUsers = Explode(",", $strUsers);
    $arrGroups = Explode(",", $strGroups);
    if (in_array($UserName, $arrUsers)) {
      $isValid = true;
    }
    // Or, you may restrict access to only certain users based on their username.
    if (in_array($UserGroup, $arrGroups)) {
      $isValid = true;
    }
    if (($strUsers == "") && false) {
      $isValid = true;
    }
  }
  return $isValid;
}

$MM_restrictGoTo = "/nimatest/admin/login.php";
if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) { 
  $MM_qsChar = "?";
  $MM_referrer = $_SERVER['PHP_SELF'];
  if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
  if (isset($QUERY_STRING) && strlen($QUERY_STRING) > 0)
  $MM_referrer .= "?" . $QUERY_STRING;
  $MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
  header("Location: ". $MM_restrictGoTo);
  exit;
}
?>
<?php
virtual('/nimatest/Connections/nimatest.php');
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $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 usertype (UserType) VALUES (%s)",
                      GetSQLValueString($_POST['UserType'], "text"));
 
  mysql_select_db($database_nimatest, $nimatest);
  $Result1 = mysql_query($insertSQL, $nimatest) or die(mysql_error());

  $insertGoTo = "/nimatest/admin/usertypes/index.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"><!-- InstanceBegin template="/Templates/Admin Layout.dwt.php" codeOutsideHTMLIsLocked="false" -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1251" />
<!-- InstanceBeginEditable name="doctitle" -->
<title>Нима - Администрация - Добави нов тип потребител</title>
<!-- InstanceEndEditable -->
<?php virtual('/nimatest/includes/header.php'); ?>
<!-- InstanceBeginEditable name="head" --><!-- InstanceEndEditable -->
<link href="/nimatest/assets/css/admin.css" rel="stylesheet" type="text/css" />
</head>
<body onload="MM_preloadImages('/nimatest/assets/images/navigation/aboutus-over.gif','/nimatest/assets/images/navigation/ourproducts-over.gif','/nimatest/assets/images/navigation/abouttea-over.gif','/nimatest/assets/images/navigation/brewingtea-over.gif')">
<?php virtual('/nimatest/includes/navigation.php'); ?>
  <tr>
    <td id="tdContent"><h1>Администрация: <!-- InstanceBeginEditable name="Section name" -->Добави нов типотребител <!-- InstanceEndEditable --> </h1>
      <!-- InstanceBeginEditable name="Body Contant" -->
      <p>Попълни формата, за да добавиш нов потребител! </p>
      <form method="post" name="form1" action="<?php echo $editFormAction; ?>">
        <table align="center">
          <tr valign="baseline">
            <td nowrap align="right">Тип потребител:</td>
            <td><input type="text" name="UserType" value="" size="32"></td>
          </tr>
          <tr valign="baseline">
            <td nowrap align="right">&nbsp;</td>
            <td><input type="submit" value="Добави нов тип потребител"></td>
          </tr>
        </table>
        <input type="hidden" name="MM_insert" value="form1">
      </form>
      <!-- InstanceEndEditable --></td>
    <td id="tdSidebar"><a href="/nimatest/admin/users/index.php"><img src="/nimatest/assets/images/navigation/bullet.gif" alt="" width="12" height="7" />Потребители </a><a href="/nimatest/admin/usertypes/index.php"><img src="/nimatest/assets/images/navigation/bullet.gif" alt="" width="12" height="7" />Типове потребител </a><a href="../logout.php"><img src="/nimatest/assets/images/navigation/bullet.gif" alt="" width="12" height="7" />Изход</a> </td>
  </tr>
<?php virtual('/nimatest/includes/footer.php'); ?>
</body><!-- InstanceEnd -->
</html>[/code]
Link to comment
Share on other sites

You have to write session_start() at the top of the page.Solution is so easy,

replace this
[code]if (!isset($_SESSION)) {
  session_start();
}[/code]

with that
[code]session_start();[/code]

that's it.You already have to start session anyway if you wanna use it in the page, you don't need to check it like that.If you wanna check a specific session then you could use the one below

[code]
session_start();
if (!isset($_SESSION['test'])) {
  //here you can do what ever you wanna do
}
[/code]
Link to comment
Share on other sites

When you have an error like that, it means that you are trying to send header information after HTML has already been sent to the browser. Once you have begun your output, you can no longer send any header information (whether that is redirects, content-types, etc). Check your included header.php file and make sure you are not trying to start any sessions or send other header information within it. Otherwise, make sure that any functions that may be using header information are not being called after your first line of HTML has been output.
Link to comment
Share on other sites

[quote author=mgallforever link=topic=123136.msg508548#msg508548 date=1169214285]
You could easily stop errors like this from happening.

At the beginning of your code have:
[code=php:0]
<?php
ob_start();
[/code]

And at the end of your code have:

[code=php:0]
ob_end_flush();
?>
[/code]
[/quote]

This is very true; however, it often encourages sloppy coding practices. There are times where your output buffering functions are your best solution, but in a case like this, you simply need to clean up your code to keep the processing and output separate enough not to cause errors.
Link to comment
Share on other sites

thanks everyone for the help! i think i know where is my error

it is in the line of code i output:

[code]
virtual('/nimatest/Connections/nimatest.php');
[/code]

and after that few lines later i do this:

[code]
  $insertGoTo = "/nimatest/admin/usertypes/index.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
[/code]

and it is creating the warning because first i virtual nimatest.php and then i make header (redirection), but how can i move nimatest.php after header and if i do that will that help? the problem is that nimatest.php is my connection to the database and on line 82 and 83 it is require a database connection:

[code]
  mysql_select_db($database_nimatest, $nimatest);
  $Result1 = mysql_query($insertSQL, $nimatest) or die(mysql_error());
[/code]

and after that follow this code:

[code]
  $insertGoTo = "/nimatest/admin/usertypes/index.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
[/code]

i tried all everyone wrote me but everything is seems fine and still gives me that error
Link to comment
Share on other sites

it may not solve your problem, but worth noting on this line, just before your HTML starts:
[code]
header(sprintf("Location: %s", $insertGoTo));
exit;
[/code]
put an exit after the header line. redirecting with header like this only gets actioned once the script ends. meaning you send the header for a new location, but then HTML is output to the browser first....I've known the infamous "header error" to crop up before in this case....
Link to comment
Share on other sites

thank you, redbullmarky

it is working now but i needed to remove this line [b]virtual('/nimatest/Connections/nimatest.php');[/b] and add the code inside nimatest.php in the page and add [b]exit;[/b] as you told me and now everything is working fine! thank you! i dont know what was cousing the problem but now everything seems to work fine! :)

thank you very much to everyone again!
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.