why php outputs <br /> if I used header() or setcookie() only with particular request

℡╲_俬逩灬. 提交于 2019-12-11 17:22:28

问题


If I used header() or setcookie() PHP outputs <br /> at the very first output !!!.

It does this only while serving the line messenger webhook message notification request -it's a messaging service I try to use- this request is similar to the PayPal-IPN . I don't know what is really so unique with this request that makes PHP behaves like this!.

for debugging purposes i did this at the very first line of the frontController php file (every request goes throw this entry point file first)

<?php
ob_start();
header("fooBar");// or setcookie()
file_put_contents("obContent_normal.txt", ob_get_contents());
//file_put_contents("obContent_line.txt", ob_get_contents()); // I use this when I handle the line notification request
exit;

This is the content of obContent_normal.txt it's filled when I make normal request from the browser

it's an empty file

and this is the content of obContent_line.txt it's filled when the line messenger server notifies my webhook with a message

<br /> <b>Warning</b>: Cannot modify header information - headers already sent in <b>C:\xampp\htdocs\domainname\public_html\frontController.SE.php</b> on line <b>3</b><br />


Dumping The 2 Requests

I tried to figure out what is so special with this line notification request, so I did that for debugging purposes

<?php
$requestData = "";
foreach ($_REQUEST as $key => $value) $requestData .= "REQUEST['$key'] => $value\n";
foreach ($_SERVER as $key => $value) $requestData .= "SERVER['$key'] => $value\n";
foreach ($_POST as $key => $value) $requestData .= "POST['$key'] => $value\n";
foreach ($_GET as $key => $value) $requestData .= "GET['$key'] => $value\n";
foreach ($_COOKIE as $key => $value) $requestData .= "COOKIE['$key'] => $value\n";
file_put_contents("request_normal.txt", $requestData);
//file_put_contents("request_line.txt", $requestData);//I use this when I handle the line notification request 
exit;

This is exactly the content of request_normal.txt. it's filled when I make normal request from the browser

SERVER['SCRIPT_URL'] => /lineWebhook
SERVER['SCRIPT_URI'] => http://localhost/lineWebhook
SERVER['MIBDIRS'] => C:/xampp/php/extras/mibs
SERVER['MYSQL_HOME'] => \xampp\mysql\bin
SERVER['OPENSSL_CONF'] => C:/xampp/apache/bin/openssl.cnf
SERVER['PHP_PEAR_SYSCONF_DIR'] => \xampp\php
SERVER['PHPRC'] => \xampp\php
SERVER['TMP'] => \xampp\tmp
SERVER['HTTP_HOST'] => localhost
SERVER['HTTP_USER_AGENT'] => Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0
SERVER['HTTP_ACCEPT'] => text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
SERVER['HTTP_ACCEPT_LANGUAGE'] => en-US,en;q=0.5
SERVER['HTTP_ACCEPT_ENCODING'] => gzip, deflate
SERVER['HTTP_REFERER'] => http://localhost/
SERVER['CONTENT_TYPE'] => application/x-www-form-urlencoded
SERVER['CONTENT_LENGTH'] => 0
SERVER['HTTP_DNT'] => 1
SERVER['HTTP_CONNECTION'] => keep-alive
SERVER['HTTP_UPGRADE_INSECURE_REQUESTS'] => 1
SERVER['PATH'] => C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\system32\config\systemprofile\.dnx\bin;C:\Program Files\Microsoft DNX\Dnvm\;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\Program Files (x86)\Skype\Phone\;C:\Program Files\PuTTY\;C:\Users\m.reda\Desktop;c:/xampp/mysql/bin/
SERVER['SystemRoot'] => C:\Windows
SERVER['COMSPEC'] => C:\Windows\system32\cmd.exe
SERVER['PATHEXT'] => .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
SERVER['WINDIR'] => C:\Windows
SERVER['SERVER_SIGNATURE'] => <address>Apache/2.4.23 (Win32) OpenSSL/1.0.2h PHP/5.6.28 Server at localhost Port 80</address>

SERVER['SERVER_SOFTWARE'] => Apache/2.4.23 (Win32) OpenSSL/1.0.2h PHP/5.6.28
SERVER['SERVER_NAME'] => localhost
SERVER['SERVER_ADDR'] => ::1
SERVER['SERVER_PORT'] => 80
SERVER['REMOTE_ADDR'] => ::1
SERVER['DOCUMENT_ROOT'] => C:/xampp/htdocs/domainname/public_html
SERVER['REQUEST_SCHEME'] => http
SERVER['CONTEXT_PREFIX'] => 
SERVER['CONTEXT_DOCUMENT_ROOT'] => C:/xampp/htdocs/domainname/public_html
SERVER['SERVER_ADMIN'] => postmaster@localhost
SERVER['SCRIPT_FILENAME'] => C:/xampp/htdocs/domainname/public_html/frontController.SE.php
SERVER['REMOTE_PORT'] => 58337
SERVER['GATEWAY_INTERFACE'] => CGI/1.1
SERVER['SERVER_PROTOCOL'] => HTTP/1.1
SERVER['REQUEST_METHOD'] => POST
SERVER['QUERY_STRING'] => 
SERVER['REQUEST_URI'] => /lineWebhook
SERVER['SCRIPT_NAME'] => /lineWebhook
SERVER['PHP_SELF'] => /lineWebhook
SERVER['REQUEST_TIME_FLOAT'] => 1512929177.986
SERVER['REQUEST_TIME'] => 1512929177

and This is exactly the content of request_line.txt. It's filled when the line messenger server notifies my webhook with a message

SERVER['SCRIPT_URL'] => /lineWebhook
SERVER['SCRIPT_URI'] => http://e9f64521.ngrok.io/lineWebhook
SERVER['MIBDIRS'] => C:/xampp/php/extras/mibs
SERVER['MYSQL_HOME'] => \xampp\mysql\bin
SERVER['OPENSSL_CONF'] => C:/xampp/apache/bin/openssl.cnf
SERVER['PHP_PEAR_SYSCONF_DIR'] => \xampp\php
SERVER['PHPRC'] => \xampp\php
SERVER['TMP'] => \xampp\tmp
SERVER['HTTP_X_LINE_SIGNATURE'] => SU5lorfDodlil02J3sn6rCI5ZlrN1N6mMv6U2KUcTGQ=
SERVER['CONTENT_TYPE'] => application/json;charset=UTF-8
SERVER['CONTENT_LENGTH'] => 236
SERVER['HTTP_HOST'] => e9f64521.ngrok.io
SERVER['HTTP_ACCEPT'] => */*
SERVER['HTTP_USER_AGENT'] => LineBotWebhook/1.0
SERVER['HTTP_X_FORWARDED_PROTO'] => https
SERVER['HTTP_X_FORWARDED_FOR'] => 203.104.146.154
SERVER['PATH'] => C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\system32\config\systemprofile\.dnx\bin;C:\Program Files\Microsoft DNX\Dnvm\;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\Program Files (x86)\Skype\Phone\;C:\Program Files\PuTTY\;C:\Users\m.reda\Desktop;c:/xampp/mysql/bin/
SERVER['SystemRoot'] => C:\Windows
SERVER['COMSPEC'] => C:\Windows\system32\cmd.exe
SERVER['PATHEXT'] => .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
SERVER['WINDIR'] => C:\Windows
SERVER['SERVER_SIGNATURE'] => <address>Apache/2.4.23 (Win32) OpenSSL/1.0.2h PHP/5.6.28 Server at e9f64521.ngrok.io Port 80</address>

SERVER['SERVER_SOFTWARE'] => Apache/2.4.23 (Win32) OpenSSL/1.0.2h PHP/5.6.28
SERVER['SERVER_NAME'] => e9f64521.ngrok.io
SERVER['SERVER_ADDR'] => ::1
SERVER['SERVER_PORT'] => 80
SERVER['REMOTE_ADDR'] => ::1
SERVER['DOCUMENT_ROOT'] => C:/xampp/htdocs/domainname/public_html
SERVER['REQUEST_SCHEME'] => http
SERVER['CONTEXT_PREFIX'] => 
SERVER['CONTEXT_DOCUMENT_ROOT'] => C:/xampp/htdocs/domainname/public_html
SERVER['SERVER_ADMIN'] => postmaster@localhost
SERVER['SCRIPT_FILENAME'] => C:/xampp/htdocs/domainname/public_html/frontController.SE.php
SERVER['REMOTE_PORT'] => 58407
SERVER['GATEWAY_INTERFACE'] => CGI/1.1
SERVER['SERVER_PROTOCOL'] => HTTP/1.1
SERVER['REQUEST_METHOD'] => POST
SERVER['QUERY_STRING'] => 
SERVER['REQUEST_URI'] => /lineWebhook
SERVER['SCRIPT_NAME'] => /lineWebhook
SERVER['PHP_SELF'] => /lineWebhook
SERVER['REQUEST_TIME_FLOAT'] => 1512929548.099
SERVER['REQUEST_TIME'] => 1512929548

Additional Info

  • This is on localhost but it's exposed to the public by https://ngrok.com/.

  • the PHP version is 5.6.28

  • windows machine with xampp
  • the line webhook url is /lineWebhook (but it is handled by the frontController file first)
  • I found similar problem here but it got no answers.

Please help me it's been hours and I can't figure out the problem alone.

Why does PHP or Apache output this <br /> only with the line notification request ?


UPDATE

as Lawrence Cherone told me in the comments I tried headers_sent() as like this

<?php
ob_start();
header("X-TEST:foobar");

if (!headers_sent($filename, $linenum)) {
    echo "Headers not sent";
} else {
    echo "Headers already sent in $filename on line $linenum";
}
file_put_contents("obContent.txt", ob_end_clean());
exit;

and this is the obContent.txt file when I do a normal request from my browser

bool(false)

and this is the obContent.txt file when the Line messenger sends the notification

<br /> <b>Warning</b>: Cannot modify header information - headers already sent in <b>C:\xampp\htdocs\accountantandprogrammer\public_html\frontController.SE.php</b> on line <b>3</b><br /> bool(true)

update

I checked ngroks introspection tool as Lawrence Cherone told me and this is my server response when I do the request from my browser normally

bool(false)

and this is my server response in the ngroks introspection tool when the request made by the Line server notification

<br />
<b>Deprecated</b>:  Automatically populating $HTTP_RAW_POST_DATA is deprecated and will be removed in a future version. To avoid this warning set 'always_populate_raw_post_data' to '-1' in php.ini and use the php://input stream instead. in <b>Unknown</b> on line <b>0</b><br />
<br />
<b>Warning</b>:  Cannot modify header information - headers already sent in <b>Unknown</b> on line <b>0</b><br />
<br />
<b>Warning</b>:  Cannot modify header information - headers already sent in <b>C:\xampp\htdocs\accountantandprogrammer\public_html\frontController.SE.php</b> on line <b>32</b><br />

来源:https://stackoverflow.com/questions/47742655/why-php-outputs-br-if-i-used-header-or-setcookie-only-with-particular-re

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!