Prevent Comments from Parsing - php

Is there a way to hide commenting in my php/html file?
I want to add markup that I don't want people to be able to view in source in their browsers.
Is this possible?
<!-- Prevent this comment from being viewed -->
<?php...
Thanks in advance.

If you add comments as PHP, people won't see it in their browser.
<div>
<!-- This HTML comment can be seen by people -->
<?php //But this PHP comment can only be seen by me :) ?>
</div>
http://en.wikipedia.org/wiki/PHP

I see what you mean. You can do that with output buffering:
<?php
// this is not
?><!-- this is sent to browser -->
And with output buffering.
<?php
ob_start();
?><!-- this is NOT sent to browser --> <b>This isn't sent as well!</b> <?php
ob_end_clean();
?>
However, if you want to remove comments only, you need to do some parsing:
<?php
ob_start();
?><!-- this is NOT sent to browser --><?php
$html=ob_get_clean();
// TODO: Use a DOM parser to parse $html and remove comments from it.
?>
That does sound a bit over-engineered though...

Just swap lines above and use php comment:
<?php...
// Prevented this comment from being viewed

Related

What may cause differences in displaing http content on server?

<section>
<?php echo(file_get_contents('http://server_address'));?>
</section><script>
<!--//--><![CDATA[// ><!--
$("ul").html('http://server_address');
//--><!]]>
</script>
What may cause that first server gets me the content of the link, the second displayed only text "http://server_address". Both servers are Apache2.
In the first case you use file_get_contents in PHP, that is actually doing what you need. The second use you just output the text. So your program is doing exactly what you wrote.
If I understood correct what you need is to output content from the link into ul element. This can be done l
<section>
<?php echo(file_get_contents('http://server_address'));?>
<!-- save content to variable, escaping quote chars -->
<?php $content = addslashes(file_get_contents('http://server_address')); ?>
</section><script>
<!--//--><![CDATA[// ><!--
$("ul").html('<?php echo $content; ?>');
//--><!]]>
</script>

Atom comments are incorrect

When I toggle comments in PHP with Atom, it always comment as HTML, even if the file extension is PHP. Example :
<!-- <?php echo "Test"; ?> -->
instead of
// <?php echo "Test"; ?>
Is there a way to fix that? I tried in atom --safe mode, but it doesn't help. Thanks!
I think that it is a normal behaviour. Because all around php tags is html code.

Adding a large amount of html and php content as PHP variable

I am trying to make my life a lot easier and make all pages have the same footer and head content from one file and this is what I have so far:
Page with content
<?php
include ("content.php");
echo $page_header;
?>
<div id="content">
</div>
<?php
echo $page_footer;
?>
content.php
<?php
// This is the header which we want to have on all pages
$page_header = include ("resources/content/header.php");
// This is the footer which we want on all pages
$page_footer = include ("resources/content/footer.php");
?>
Header.php Example
<html>
<head>
<title>This is my title</title>
</head>
<body>
<div id="logo">
</div>
Footer.php Example
<div id="footer">Copyright to me!</div>
</body>
</html>
The problem I am having is my header.php content isn't all displaying and causing issues with the page formatting. The header.php does include some php if statements and some in-line javascript... should this matter?
Is there a better way of doing it?
PLEASE NOTE: I am using PHP 5 locally and my server is PHP 4 so the answer needs to work for both
One way is to use output buffering functions for that.
Change content.php file:
ob_start();
include ("resources/content/header.php");
$page_header = ob_get_clean();
ob_start();
include ("resources/content/footer.php");
$page_footer = ob_get_clean();
ob_start() function creates a temporary buffer for any output, then include() makes it's output not to page response, but to buffer that have been created by ob_start(). ob_get_clean() collects contents of a buffer, destroys it and returns collected data as a string.
Another way as mentioned by #u_mulder is to simply include() those files right where they are needed.
Change page with content file:
<?php include ("resources/content/header.php"); ?>
<div id="content">
</div>
<?php include ("resources/content/footer.php"); ?>
However in some time you'll might need some complex template processing engine. There are plenty of them for php.

Is there anyway to produce readable HTML source from PHP?

I'm not very experienced at php, so if there's an easy function for this, I'll feel like an idiot.
When coding in PHP, at the point where you need to echo some HTML code, I have found I have either one of two options.
A: echo "<!--html text here-->";
B: echo "\t\t\t<!--html text here-->\n";
If I were to use method A throughout the code, looking at the php code from client side using view-source produces a solid block of code which is difficult to read.
If I were to use method B, it looks fine client-side, but the actual source code looks messy.
Is there anyway to keep both server and client-side appearance clean?
Something like this
<?php
//php code here
?>
//html code
<h3> <?= $justADynamicVariable ?> </h3>
<?php
//continue php code
?>
Un-readable front end code is caused by poor factoring of your PHP code
The wall-of-text issue occurs because your code is messy. You have logic and display code in the same place (evident from the fact that you're echoing HTML from a PHP block) and this will always produce unreadable and ugly code.
Look at Model-View-Controller pattern, and separate out your logic code from your display code. Then, write your display code in a primarily HTML format with some in-line PHP:
<div>
Welcome back <?= $this->username; ?>
</div>
If you're having to echo HTML code from a PHP block, your code is probably factored wrong.
Other useful tricks to produce readable code:
Use alternative PHP control blocks
This:
<div id='somediv'>
<?php if($something): ?>
Some stuff
<?php endif; ?>
</div>
is infinitely more readable than this:
<div id='somediv'>
<?php if($something) { ?>
Some stuff
<?php } ?>
</div>
And it's definitely better than this which is probably what you're using now:
<?php
echo "<div id='somediv'>";
if($something) {
echo "Some stuff";
}
echo "</div>";
?>
You can just close the php tag (?>), write the html block and return to php (
Use a templating engine, like Twig http://twig.sensiolabs.org/ it allows you to separate your logic out of your templates so you can write really easy to markup with simple syntax to drop in dynamic variables.
or
Use a browser like Chrome, which will auto indent your source for you when you use the Web Developer Tools http://discover-devtools.codeschool.com/. You can even copy paste the results really quickly to a new file. In general, it's a bad idea to fret over outputting well spaced html since software can clean it up so easily.
There are lot's of ways to produce html, depending or it's a single line or an entire block of HTML.
The important thing is readability.
echo "<div><p>Hi ".$name."</p></div>";
Isn't unreadable persé, it might be annoying because it won't get highlighted in many IDE's.
This is a good reason to always seperate html and php, meaning always put html parts outside of your php tags.
So you could end your php block before and open it again after :
if(TRUE) {
?>
<div><p>Hi <?php echo $name;?></p></div>
<?php
}
If you have PHP within a large file mostly containing html you are better of using php control blocks:
<!--html text here-->
<?php if(TRUE):?>
<div>
<p>Hello <?php echo $name;?></p>
</div>
</php endif;?>
<!--html text here-->
You could also use heredoc
echo <<<EOD
<div>
<p>This is html</p>
</div>
EOD;
Or output buffering:
ob_start();
?>
<div>
<p>Hello <?php echo $name;?></p>
</div>
<?php
$html = ob_get_clean();
echo $html;

How do HTML comments work with PHP code?

I just wonder how does html comment tag work with php code.
If I have the following code, does php parse it anyway?
<!--
<?php
echo "hi";
?>
-->
a simple question, but I guess it is important to know.
Oh yes, anything in PHP blocks gets executed, but in this case, it isn't shown to the end user because it's in HTML comments.
In the source of the page that is generated by this PHP script, you will see the output surrounded by HTML comments, simple as that.
The only way comments will affect the output of a PHP script is with valid PHP comments.
Yes it does. If that is between <?php ?> tags
You can use PHP comments /* commment */ and they won't execute.
As a side note to the answers above: You can also interrupt the HTML comment:
<!--
<?php
echo "-->This will be seen!<!--";
?>
-->
gives this output:
<!--
-->This will be seen!<!---->

Categories