development

경고 : 헤더 정보를 수정할 수 없습니다. 헤더는 이미 ERROR [duplicate]에 의해 전송되었습니다.

big-blog 2020. 9. 17. 08:26
반응형

경고 : 헤더 정보를 수정할 수 없습니다. 헤더는 이미 ERROR [duplicate]에 의해 전송되었습니다.


중복 가능성 : 이미 PHP에서 보낸 헤더

나는 잠시 동안이 오류로 고심하고 있습니다.

우선 공백이라고 생각했지만 추가 조사를 마친 후 다음과 유사한 문제가 될 수 있다고 생각합니다.

이 헤더 문 앞에 사용자에게 출력을 보낼 수있는 문을 찾습니다. 하나 이상을 찾으면 코드를 변경하여 헤더 문을 앞쪽으로 이동하십시오. 복잡한 조건문은 문제를 복잡하게 만들 수 있지만 문제 해결에 도움이 될 수도 있습니다. 가능한 한 빨리 헤더 값을 결정하고 거기에 설정하는 PHP 스크립트 상단의 조건식을 고려하십시오.

포함 헤더가 header ()와 함께 문제를 일으키고 있다고 생각하지만이 오류를 제거하기 위해 코드를 재배 열하는 방법을 모르겠습니다.

오류를 어떻게 제거합니까?

<?php
    $username = $password = $token = $fName = "";

    include_once 'header.php';

    if (isset($_POST['username']) && isset($_POST['password']))
        $username = sanitizeString($_POST['username']);

    $password = sanitizeString($_POST['password']); //Set temporary username and password variables
    $token    = md5("$password"); //Encrypt temporary password

    if ($username != 'admin')
    {
        header("Location:summary.php");
    }
    elseif($username == 'admin')
    {
        header("Location:admin.php");
    }
    elseif($username == '')
    {
        header("Location:index.php");
    }
    else
        die ("<body><div class='container'><p class='error'>Invalid username or password.</p></div></body>");

    if ($username == "" || $token == "")
    {
        echo "<body><div class='container'><p class='error'>Please enter your username and password</p></div></body>";
    }
    else
    {
        $query = "SELECT * FROM members WHERE username='$username'AND password = '$token'"; //Look in table for username entered
        $result = mysql_query($query);
        if (!$result)
            die ("Database access failed: " . mysql_error());
        elseif (mysql_num_rows($result) > 0)
        {
            $row = mysql_fetch_row($result);
            $_SESSION['username'] = $username; //Set session variables
            $_SESSION['password'] = $token;

            $fName = $row[0];
        }
    }
?>

장기적인 대답은 PHP 스크립트의 모든 출력이 변수에 버퍼링되어야한다는 것입니다. 여기에는 헤더 및 본문 출력이 포함됩니다. 그런 다음 스크립트 끝에서 필요한 출력을 수행하십시오.

문제에 대한 매우 빠른 수정은 다음을 추가하는 것입니다.

ob_start();

이 하나의 스크립트에서만 필요한 경우 스크립트의 맨 처음으로. 모든 스크립트에 필요한 경우 header.php 파일에서 가장 먼저 추가하십시오.

This turns on PHP's output buffering feature. In PHP when you output something (do an echo or print) it has to send the HTTP headers at that time. If you turn on output buffering you can output in the script but PHP doesn't have to send the headers until the buffer is flushed. If you turn it on and don't turn it off PHP will automatically flush everything in the buffer after the script finishes running. There really is no harm in just turning it on in almost all cases and could give you a small performance increase under some configurations.

If you have access to change your php.ini configuration file you can find and change or add the following

output_buffering = On

This will turn output buffering out without the need to call ob_start().

To find out more about output buffering check out http://php.net/manual/en/book.outcontrol.php


Check something with echo, print() or printr() in the include file, header.php.

It might be that this is the problem OR if any MVC file, then check the number of spaces after ?>. This could also make a problem.


You are trying to send headers information after outputing content.

If you want to do this, look for output buffering.

Therefore, look to use ob_start();


There are some problems with your header() calls, one of which might be causing problems

  • You should put an exit() after each of the header("Location: calls otherwise code execution will continue
  • You should have a space after the : so it reads "Location: http://foo"
  • It's not valid to use a relative URL in a Location header, you should form an absolute URL like http://www.mysite.com/some/path.php

참고URL : https://stackoverflow.com/questions/9707693/warning-cannot-modify-header-information-headers-already-sent-by-error

반응형