This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Reference - What does this error mean in PHP?
(38 answers)
Closed 9 years ago.
When I run player.php it's giving this error:
Warning: Cannot modify header information - headers already sent by (output started
at /www/110mb.com/m/u/s/i/c/k/i/n/musicking/htdocs/player.php:8) in
/www/110mb.com/m/u/s/i/c/k/i/n/musicking/htdocs/player.php on line 24
Can you please help?
<!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>Player</title>
</head>
<body>
<?php
if(isset($_POST["song"])&& $_POST['song'] != "")
{
$song = $_POST["song"];
}
else {$song=array();}
for ($i="0"; $i<count($song); $i++) {
}
//start of new php codep
// create doctype
//$array = array(
// 'song.mp3','song.mp3','song.mp3',
//);
$dom = new DOMDocument("1.0");
// display document in browser as plain text
// for readability purposes
header("Content-Type: text/plain");
// create root element
$root = $dom->createElement("xml");
$dom->appendChild($root);
$i = "1";
foreach ($song as $counter) {
// create child element
$song = $dom->createElement("track");
$root->appendChild($song);
$song1 = $dom->createElement("path");
$song->appendChild($song1);
// create text node
$text = $dom->createTextNode($counter);
$song1->appendChild($text);
$song1 = $dom->createElement("title");
$song->appendChild($song1);
$text = $dom->createTextNode("song ".$i);
$song1->appendChild($text);
$i++;
}
// save and display tree
$dom->save("playlist.xml");
?>
<script type="text/javascript" src="swfobject.js">
</script>
<div id="flashPlayer">
This text will be replaced by the flash music player.
</div>
<script type="text/javascript">
var so = new SWFObject("playerMultipleList.swf", "mymovie", "295", "200", "7", "#FFFFFF");
so.addVariable("autoPlay","yes")
so.addVariable("playlistPath","playlist.xml")
so.write("flashPlayer");
</script>
</body>
</html>
The error message is triggering because of the HTML that appears before your first <?php tag. You cannot output anything before header() is called. To fix this error start your document with the <?php tag and only start outputting HTML after you are done handling the condition that outputs XML for flash.
A cleaner solution would be to separate out the XML generation for flash and the HTML output into different files.
The error message means that the php script has already sent output to the browser before calling the header() function or anything else that requires modifying the http headers.
it is really hard to try and diagnose where the problem is occuring without see the script properly formatted, but this line:
header("Content-Type: text/plain");
should be at the start of the script in php tags.
Seems that you're trying to use a Flash MP3 Player, but you're mixing up some things.
You're generating the XML playlist file on the same file that you have the player, you could do it, but I think that will be clearer and simpler to have lets say, a genPlayList.php file that will generate the XML file for you.
Then in your MP3 Player page you can have only a reference to that script:
....
so.addVariable("playlistPath","genPlayList.php");
....
like nav says, it means output has already been sent. In this case it's all the
<!DOCTYPE html PUBLIC ...
....
<body>
you got going on there.
You should move the entire php processing block before this.
Try to use javascript redirect instead of redirect with header.
Related
Having the next PHP code that produce HTML code:
(simplified function, the real one on same idea but longer with loops and so on):
<?php
function show_doc_html() {
$text_to_title = "some text from db";
?>
<html>
<head>
<title>
<?php echo $text_to_title ?>
</title>
</head>
<body>
</body>
</html>
<?
}
I would like to return a PDF to the user, without changing too much of this code. we are working under drupal so we have function that can get html string and convert it to pdf, but the former function doesn't return anything but printing to stdout. Id it possible? or should i rebuild the old function to return string?
Is that what you need?
I have used it in my project. You can just write your markup and inline css in node template using view_mode = 'PDF'
The easiest way was to encapsulate my function with "ob_start()" get all text using "ob_get_contents()", then convert it to pdf.
Something like:
function show_doc_pdf() {
ob_start();
function show_doc_html() ;
$html_var = ob_get_contents();
ob_end_clean();
//use wkhtmltopdf api on $html_var to return pdf
}
I used #Alex's approach here to remove script tags from a HTML document using the built in DOMDocument. The problem is if I have a script tag with Javascript content and then another script tag that links to an external Javascript source file, not all script tags are removed from the HTML.
$result = '
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>
hey
</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
alert("hello");
</script>
</head>
<body>hey</body>
</html>
';
$dom = new DOMDocument();
if($dom->loadHTML($result))
{
$script_tags = $dom->getElementsByTagName('script');
$length = $script_tags->length;
for ($i = 0; $i < $length; $i++) {
if(is_object($script_tags->item($i)->parentNode)) {
$script_tags->item($i)->parentNode->removeChild($script_tags->item($i));
}
}
echo $dom->saveHTML();
}
The above code outputs:
<html>
<head>
<meta charset="utf-8">
<title>hey</title>
<script>
alert("hello");
</script>
</head>
<body>
hey
</body>
</html>
As you can see from the output, only the external script tag was removed. Is there anything I can do to ensure all script tags are removed?
Your error is actually trivial. A DOMNode object (and all its descendants - DOMElement, DOMNodeList and a few others!) is automatically updated when its parent element changes, most notably when its number of children change. This is written on a couple of lines in the PHP doc, but is mostly swept under the carpet.
If you loop using ($k instanceof DOMNode)->length, and subsequently remove elements from the nodes, you'll notice that the length property actually changes! I had to write my own library to counteract this and a few other quirks.
The solution:
if($dom->loadHTML($result))
{
while (($r = $dom->getElementsByTagName("script")) && $r->length) {
$r->item(0)->parentNode->removeChild($r->item(0));
}
echo $dom->saveHTML();
I'm not actually looping - just popping the first element one at a time. The result: http://sebrenauld.co.uk/domremovescript.php
To avoid that you get the surprises of a live node list -- that gets shorter as you delete nodes -- you could work with a copy into an array using iterator_to_array:
foreach(iterator_to_array($dom->getElementsByTagName($tag)) as $node) {
$node->parentNode->removeChild($node);
};
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Calling PHP functions within HEREDOC strings
I am using Swiftmailer PHP to create an order complete page. When the customer lands on this page, I use an include to a PHP file.
This page file has SwiftMailer EOM HTML that gets sent to the customer. However I have the HTML parts in chunks, so the header is via a function called header and order totals are the same too. I want to be able to include EOM functions inside the EOM. Is this possible?
Id = 46088;
// MAIL FUNCTION
function mailToSend($Id){
// EOM CAN'T BE TABBED
$html = <<<EOM
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
getHeader($Id);
getOrderTotalBoxTable($Id);
</body>
</html>
EOM;
}
mailToSend(46088);
Like #deceze said, you can't, but this will work (extend #xenon comment to an example):
function getHeader($Id = '')
{
$text = '';
$text.=' Your first line of text, store it in an variable <br>';
$text.= 'Hello '.$Id.'<br>';
$text.='Your last text to be returned<br>';
return $text;
}
// MAIL FUNCTION
function mailToSend($Id){
$getHeader = getHeader($Id);
$getOrderTotalBoxTable = getOrderTotalBoxTable($Id);
// EOM CAN'T BE TABBED
$html = <<<EOM
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
$getHeader;
$getOrderTotalBoxTable;
</body>
</html>
EOM;
}
mailToSend(46088);
What you're talking about are Heredocs and they don't support function interpolation, only variable interpolation.
I'm trying to echo a PHP tag by doing this:
echo "<?php echo \"test\"; ?>";
The result should be just "test" without quotes, but my code isn't working. What is happening is that nothing is shown on the page, but the source code is "<?php echo "teste"; ?>"
Most of you will want to know why I want to do this. I'm trying to make my own template system; the simplest way is just using file_get_contents and replacing what I want with str_replace and then using echo.
The problem is, that in the template file, I have to have some PHP functions that doesn't work when I echo the page, is there another simple way to do this? Or if you just answer my question will help a lot!
Here is an example of what I am trying to accomplish:
template.tpl:
<!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>[__TITULO__]</title>
</head>
<body >
<p>Nome: [__NOME__] <br />
Email: [__EMAIL__]<br />
<?php
if ($cidade != "") {?>
Cidade: [__CIDADE__]<br />
<?php
}
?>
Telefone: ([__DDD__]) [__TELEFONE__] <br />
Fax:
([__DDDFAX__]) [__FAX__] <br />
Interesse: [__INTERESSE__]<br />
Mensagem:
[__MENSAGEM__] </p>
</body>
</html>
index.php
<?php
$cidade = "Teste";
$file = file_get_contents('template.php');
$file = str_replace("[__TITULO__]","Esse Título é téste!", $file);
$file = str_replace("[__NOME__]","Cárlos", $file);
$file = str_replace("[__EMAIL__]","moura.kadu#gmail.com", $file);
if ($cidade != "") {
$file = str_replace("[__CIDADE__]",$cidade, $file);
}
echo $file;
?>
I can solve all this just not showing the div that has no content. like if i have a template, and in it i have 2 divs:
<div id="content1">[__content1__]</div>
<div id="content2">[__content2__]</div>
if the time that i set the content to replace the template I set the content1 and not set content 2 the div content2 will not show...
Use htmlspecialchars
That will convert the < > to < and >
You are dealing with two sets of source code here that should never be confused - the server code (PHP, which is whatever is in the <?php ?> tags) and the client (or browser) code which includes all HTML tags. The output of the server code is itself code that gets sent to the browser. Here you are in fact successfully echoing a PHP tag, but it is meaningless to the browser, which is why the browser ignores it and doesn't show anything unless you look at the client code that got sent to it.
To implement templates in this style, either they should not have any PHP code, or the resulting string (which you have stored in $file) should itself be executed as though it were PHP, rather than echoing it straight to the client. There are various ways to do this. One is to parse out the PHP tags in the string, echo everything that is not within the PHP tags and run eval() on everything that is.
I need to count the no. of lines of inline java script between script tags in php files. How do I do it? Will grep linux command suffice or I can get some tool to do it? Please help.
You might use a regular expression like to extract the content of each SCRIPT tag in your files and than count the \n occurrences within the content.
This regex should match all script tag including the opening and closing tag:
/<script[^>]*?>(.*)?</script>/sm
You should than remove the tags and lines without any code to count the real lines of JavaScript code.
Please take a look on the following code,it works but you may need to updates as per your requirements
<?php
$file = file('thisfile.php');
for($i=0;$i<count($file);$i++)
{
if(trim($file[$i]) == "<script language=\"javascript\">")
{
$start = $i.'<br>';
}
if(trim($file[$i]) == "</script>")
{
$end = $i.'<br>';
}
}
?>
<!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>
<script language="javascript">
var a = 10;
var b = 10;
var c = 10;
var d = 10;
var e = 10;
var e = 10;
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php echo ($end - $start)-1; ?>
</body>
</html>
save this php file as thisfile.php then try
Have a nice day
If you need to do this from a processed HTML page, use DOM to get all script tags without a src attribute. Then for each found node split the child TextNode by the linebreak or simple count them. Done.
If you need to grab this from actual PHP source code, use the Tokenizer to find T_STRINGS and similar parser tokens and analyze them for <script> blocks, but note that it might be impossible to find them all, if there is code like:
echo '<' . $scriptTag . '>' . $code . '</' . $scriptTag . '>';
because that wont be analyzable as a JavaScript String before PHP processed it.