Php code being commented out

I was under the impression I could use PHP code in an html widget or place it into the meta-data header or into the body. But part of my code seems to be being commented out, no matter where I try placing it. Any thoughts or suggestions?


Yes, PHP code does get commented out by the HTML widget. I’m going to have to connect with other members of the team in order to see if there’s a way around this. I’ll let you know what I find.


I really appreciate it Nathaniel.


Are you seeing it commented out in the source of the page when previewing the page, publishing the page, or both?

In the source of the page on preview. It’s not even commenting out the whole of the code though, only about 3/4th’s of it, so the rest shows up on the page as jargon. It responds the same way on publishing.


I was playing around with some PHP and I think I found what you are referring to. When I paste in a block of PHP with a greater than angle bracket “>”, I see part of the PHP appear on the page. This is happening because CM1 does not execute PHP or other server code. The execution of the code is dependent on the web server. Typically, this is Apache or IIS in which PHP can be installed or enabled. Because CM1 does not recognize PHP, it sees the “>” and treats it as a closing tag, which causes everything after to appear in the doc.

A safe method to avoid this from happening is to add commented HTML comments inside the PHP so the browser or editors that don’t support executing PHP will comment out the code. When it’s on a server supporting PHP, it will execute it properly and comment out the HTML comments to prevent that from affecting.

Instead of a typical PHP tag:

php code here ?\> you would place: <?php
/\*\>\*/ ?\>

Thanks Daved,

I cannot put this into an HTML widget, because it will comment out other stuff on the page! Almost gave myself a heart attack thinking i deleted a whole sidebar off my template. I cannot put this in my head, because it comments out the whole of the page. Placing it before the body tag gets me this:

<!--?php /*-->   
<br /> if(isset($_POST['displayThanks'])){ <br />
$displayThanks = $_POST['displayThanks'];
<br />
$commentType = $_POST['selInquiryType'];
<br />
$comments = $_POST['txtQoC'];
<br />
$Name = $_POST['Name'];
<br />
$email = $_POST['email']; <br />
  $recipient = "";
<br />
$mail_body = "From: ".$Name."\r\n\r\nEmail:" .$email. "\r\n\r\nType of Comment: ".$commentType."\r\n\r\nComment:\r\n".$comments."\r\n";
<br />
$subject = "U.S. Capitol Visitor Center Web Site"; <br />
$header = "From: ".$Name." <".$email.">\r\n";
<br />
if($displayThanks == "thanks") {
<br />
ini_set('sendmail_from', ''); <br />
mail($recipient, $subject, $mail_body, $header);
<br /> } <br /> } <br /> /*><!--*/
?> <br /><br />
   </body> <br />
</html> <br /> -->   

The extra comment tags are not something I have added. That is the way it is producing on the page.

What are we thinking?


If you look at what I pasted, the comments are actually inside the PHP code with PHP comments around them. It should not affect anything on the page. Only what’s inside the PHP tags.

<?php <br /> /\*\><!--*/ <br />
  php code here <br /> /*-->\*/   

I did excatly as you instructed, you have access to the code I am working on, please feel free to confirm it and check for yourself. What I pasted above is the result I got when I did so. That is why I mentioned: The extra comment tags are not something I have added. That is the way it is producing on the page.

So yes, it is affecting other things on the page.

I just want to make it really really clear. I am not adding the extra comment tags you see. Only the ones you told me too.

Although - maybe i got the second one wrong looking over it again, I this i accidentally used the same one for the first and last.

Okay, I fixed the second comment line as I had copied and pasted, however the php code is still appearing inside comment tags that I did not add.

At least it’s not commenting out only half of the code, but all of the code now. This fixed portions of my page going missing.

Still not getting the php to work.

I put the block of php into a test page in your environment using the starting tags outlined. I added the code to the “Additional code” section in the Head, After Body Open, and Before Body Close section to provide an example for you to reference. I also reviewed your page with the code in it and it appears to be correct now. The content on the page is showing fine and the PHP code is not showing on the page at all. I didn’t place it in a HTML widget but it will have the same effect.

The expected result is that the PHP is not showing on the page in preview or local publish, but the rest of the page content should appear normal. As mentioned in my previous post, CM1 will not execute PHP code because CM1 does not recognize PHP. The web server for the published site must have PHP installed and configured on it, as can be done with Apache and IIS, for it to execute. Because of this, the hiding of the code in Edit and Preview mode is ideal, with the understanding that it will execute when the page is published to the web server supporting PHP.

Alright, well thanks for all your help on it. I will plug the webmasters email back into it since I cannot test it. Thanks.

I also found a method that might allow PHP to run within a Tomcat instance, which may allow testing PHP in a locally published site. We are going to test a configuration to see if it works. For now, the above methods prevent negative affects on the site. Additionally, the method outlined above will also allow the PHP to execute properly without modification when it publishes live to a PHP enabled web server.

Alright, great, well I appreciate all this. Sorry for my user error earlier! I would prefer to be able to test, but if it’s not in the game plan then we change the game plan!