Coding style question. PHP.

Status
Not open for further replies.
So I've got a question about coding style. In many examples around the Interweb I've seen people flopping back and forth between breaking in and out of PHP in their HTML code to display their data, or having PHP spew out all of the necessary HTML.

Code:
<html>
<body>
<h1>Stuff!</h1>
<p>Now I'm going to do some PHP to fill in the blanks! <?php echo $somevariables; ?> Wasn't that fun?</p>
</body>
</html>

vs

Code:
<?php
echo "<html>
<body>
<h1>Stuff!</h1>
<p>Now I'm going to do some PHP to fill in the blanks!" . $somevariables . "Wasn't that fun?</p>
</body>
</html>
";
?>


Then there's heredoc, which I hear is memory intensive and generally shouldn't be used.

Code:
<?php
echo <<<HTML
<html>
<body>
<h1>Stuff!</h1>
<p>Now I'm going to do some PHP to fill in the blanks! {$somevariable} Wasn't that fun?</p>
</body>
</html>
HTML;
?>


So after all of that, here's my boggle. What's the Right Way To Do It(tm)? I must admit, if left to my own devices, I use heredoc. But if that's not an option, I choose to have PHP spew out everything. Breaking in and out of PHP looks ugly and is more difficult to read in vim or NetBeans due to the lack of proper color coding. Or am I fretting too much about this? Are they all valid life choices as long as I pick one and stick with it?
 

KrispyK

Ars Scholae Palatinae
911
Use method 1. I don't see any point in using templating engines (in this instance) when PHP is good enough to do it itself.

We split our backend code and display code into seperate files:

somefile.php
Code:
<?php

  // do some work here
  $somevar = 'something';

  // if you're processing something from user input, properly escape it etc
  $userName = htmlspecialchars($_REQUEST['name']);

  // include the 'template' file to render the output
  require 'somefile.phtml';

somefile.phtml
Code:
  <h1>Stuff!</h1>
  <h2>Hello <?php echo $userName; ?></h2>
  <p>Now I'm going to do some PHP to fill in the blanks! <?php echo $somevar; ?> Wasn't that fun?</p>

Breaking in and out of PHP looks ugly and is more difficult to read in vim or NetBeans due to the lack of proper color coding.
Try using Eclipse PDT
 

Schluppy

Ars Tribunus Militum
2,486
TheEternalVortex":gm2jd0cp said:
Schluppy":gm2jd0cp said:
TheEternalVortex":gm2jd0cp said:
Doing it any of these ways is a good way to guarantee XSS holes.

How so?

Neither of the examples is escaping $somevariables.

Is there some invisible example code, assigning un-escaped values to the example variables, somewhere in the OP that I can't see? If so, you may be right. If not, your reply is misleading and irrelevant to the OP.
 

Bhelyer

Well-known member
2,231
Schluppy":21rpwhbj said:
TheEternalVortex":21rpwhbj said:
Schluppy":21rpwhbj said:
TheEternalVortex":21rpwhbj said:
Doing it any of these ways is a good way to guarantee XSS holes.

How so?

Neither of the examples is escaping $somevariables.

Is there some invisible example code, assigning un-escaped values to the example variables, somewhere in the OP that I can't see? If so, you may be right. If not, your reply is misleading and irrelevant to the OP.

They're assuming the styles would continue on with similar usages. It's neither misleading nor irrelevant, and your use of italics is deplorable. So neener neener. :p
 

TheEternalVortex

Ars Praefectus
3,094
Subscriptor
Schluppy":3q7dxo1s said:
TheEternalVortex":3q7dxo1s said:
Schluppy":3q7dxo1s said:
TheEternalVortex":3q7dxo1s said:
Doing it any of these ways is a good way to guarantee XSS holes.

How so?

Neither of the examples is escaping $somevariables.

Is there some invisible example code, assigning un-escaped values to the example variables, somewhere in the OP that I can't see? If so, you may be right. If not, your reply is misleading and irrelevant to the OP.

The point is that you don't know whether the values are escaped or not. Is that really the guarantee you want, that "we don't know that there are XSS holes"?
 

TheEternalVortex

Ars Praefectus
3,094
Subscriptor
My real point is that forcing everyone to remember to always escape everything is a terrible approach. The templating code should automatically escape data for you. I find it odd that the same people that clearly see that mysql_query("SELECT foo FROM place WHERE user = $user_name") is bad don't see the same problem with the templating layer.
 

Schluppy

Ars Tribunus Militum
2,486
TheEternalVortex":2ykmpiog said:
The point is that you don't know whether the values are escaped or not. Is that really the guarantee you want, that "we don't know that there are XSS holes"?
In a topic about style conventions? Sure.

My point is that the example styles do not create XSS vulnerabilities. Failing to escape variables does.

Had you stated something along the lines of "be sure to escape your vars else you'll be open to XSS attacks" rather than "examples bad!" we wouldn't be off on this tangent.

TheEternalVortex":2ykmpiog said:
I find it odd that the same people that clearly see that mysql_query("SELECT foo FROM place WHERE user = $user_name") is bad don't see the same problem with the templating layer.
I do see the same problem with templating I just don't see how it's is relevant to a thread about style conventions.

Also, I've emphasized every second word in this sentence just for Bhelyer.
 

TheEternalVortex

Ars Praefectus
3,094
Subscriptor
A.W.":6rdnyo57 said:
Escape by default, and force the developer to explicitly declare that he wants certain elements to *not* be escaped.

Yes, or even better don't allow unescaped data at all.

In a topic about style conventions? Sure.

My point is that the example styles do not create XSS vulnerabilities. Failing to escape variables does.
But using either of the two proposed styles makes it impossible to autoescape variables.
 

splitview

Seniorius Lurkius
7
Blacken00100":1uppon0x said:
They're all invalid and shitty life choices. The right way to do it is to taze yourself if you want to embed HTML in your PHP.


(I'm not joking. Use something like Twig for rendering HTML. Separate your concerns.)

Using PHP as your templating engine does not mean that you are not separating your concerns. See Kohana, Yii, CakePHP, CodeIgniter etc.
 

Blacken00100

Ars Legatus Legionis
10,130
Subscriptor
splitview":v1itiiuv said:
Using PHP as your templating engine does not mean that you are not separating your concerns. See Kohana, Yii, CakePHP, CodeIgniter etc.
Interesting that you cite two mediocre and two execrable frameworks as proof of this. Never mind that sandboxing PHP-as-templates is nearly fucking impossible and that content escaping strategies look like mysql_real_escape_string() only worse. Clearly the people who build tools that don't suck, e.g. @fabpot and crew, are using Twig just for their health.

I'll bet you just love extract(), too, don't'cha?
 

splitview

Seniorius Lurkius
7
Blacken00100":1g843aif said:
splitview":1g843aif said:
Using PHP as your templating engine does not mean that you are not separating your concerns. See Kohana, Yii, CakePHP, CodeIgniter etc.
Interesting that you cite two mediocre and two execrable frameworks as proof of this. Never mind that sandboxing PHP-as-templates is nearly fucking impossible and that content escaping strategies look like mysql_real_escape_string() only worse. Clearly the people who build tools that don't suck, e.g. @fabpot and crew, are using Twig just for their health.

I'll bet you just love extract(), too, don't'cha?

You're going off on a tangent. My point still stands.
 

Blacken00100

Ars Legatus Legionis
10,130
Subscriptor
No, thank you, it very much is not a tangent. Visibility of both code and data that Should Not Be In Templates is a major problem with "use PHP in the frontend" people and consistent and proper escaping of said code is not much better unless you really really like altering the global state just for the sake of using <?php ?> in your templates. These are problems that start at "nontrivial" and more or less leap straight to "intractable" in very, very short order.

Your "point" is asking for a security and maintainability fuckterclust. Just because you can does not mean you should.
 

splitview

Seniorius Lurkius
7
Blacken00100":3ooagl0l said:
No, thank you, it very much is not a tangent. Visibility of both code and data that Should Not Be In Templates is a major problem with "use PHP in the frontend" people and consistent and proper escaping of said code is not much better unless you really really like altering the global state just for the sake of using <?php ?> in your templates. These are problems that start at "nontrivial" and more or less leap straight to "intractable" in very, very short order.

Your "point" is asking for a security and maintainability fuckterclust. Just because you can does not mean you should.

Ah, classic silver bullet thinking: Here, add this new layer on top, and all your problems will be solved. You won't even need to think - all your problems just go away!
  • Proper output escaping can be done in PHP.
  • Separation of concerns has nothing to do with templating engines. Using one does not guarantee good design.
 

Blacken00100

Ars Legatus Legionis
10,130
Subscriptor
Yebus. Yes, of course proper escaping can be done in PHP. That said, should you think every developer working on a project will always and forever wrap every single fucking variable usage (and to forestall the stupid "but you only need to escape user input!" lolnonsense, why take the risk of misanalyzing what is user input and what is not when the sane option is to cover all usages?) in a call to html_purify without fail, then you quite literally shouldn't be programming things other people must use because you are a danger to those around you. (The alternative is then to heavily modify the global state just for the sake of using PHP in your templates, which is hilaristupid when there are tools, i.e. Twig, to do that for you.)

And of course it doesn't guarantee good design. But it addresses 99.9% of cases better and smarter than your psychotic "wrap everything yourself!" brain damage. Do you use mysql_real_escape_string too? Or do you just roll your own "SQL saferizer!" like everyone else who thinks their shit doesn't stink?

"Classic silver bullet thinking". Who the hell are you kidding?
 

Tornadus

Ars Scholae Palatinae
1,214
Blacken00100":ducz9cmg said:
Yebus. Yes, of course proper escaping can be done in PHP. That said, should you think every developer working on a project will always and forever wrap every single fucking variable usage (and to forestall the stupid "but you only need to escape user input!" lolnonsense, why take the risk of misanalyzing what is user input and what is not when the sane option is to cover all usages?) in a call to html_purify without fail, then you quite literally shouldn't be programming things other people must use because you are a danger to those around you. (The alternative is then to heavily modify the global state just for the sake of using PHP in your templates, which is hilaristupid when there are tools, i.e. Twig, to do that for you.)

And of course it doesn't guarantee good design. But it addresses 99.9% of cases better and smarter than your psychotic "wrap everything yourself!" brain damage. Do you use mysql_real_escape_string too? Or do you just roll your own "SQL saferizer!" like everyone else who thinks their shit doesn't stink?

"Classic silver bullet thinking". Who the hell are you kidding?

++

I also think a lot of people here need to understand what "separation of concerns" actually means.
 

splitview

Seniorius Lurkius
7
Blacken00100":gnjfox80 said:
Yebus. Yes, of course proper escaping can be done in PHP. That said, should you think every developer working on a project will always and forever wrap every single fucking variable usage (and to forestall the stupid "but you only need to escape user input!" lolnonsense, why take the risk of misanalyzing what is user input and what is not when the sane option is to cover all usages?) in a call to html_purify without fail, then you quite literally shouldn't be programming things other people must use because you are a danger to those around you. (The alternative is then to heavily modify the global state just for the sake of using PHP in your templates, which is hilaristupid when there are tools, i.e. Twig, to do that for you.)

I actually don't mind manual escaping. I think of it as manually managing memory. If I have to, I will. It's not a big deal. I'm not religious about it.

And of course it doesn't guarantee good design. But it addresses 99.9% of cases better

And PHP can't?

Yes, third party templating engines can help you enforce discipline. I pushed through the adoption of one at my last job. However, nothing replaces proper developer education. I've seen people take liberties with templating engines. Like all tools, they can also be misused. As far as I'm concerned, adoption of a third party templating engine does not equate to good design.

and smarter than your psychotic "wrap everything yourself!" brain damage. Do you use mysql_real_escape_string too? Or do you just roll your own "SQL saferizer!" like everyone else who thinks their shit doesn't stink?

"Classic silver bullet thinking". Who the hell are you kidding?

This is not a design issue. And there's no need to resort to ad hominems.
 

levine

Ars Tribunus Militum
1,986
splitview":3g34vkv8 said:
I actually don't mind manual escaping. I think of it as manually managing memory. If I have to, I will. It's not a big deal. I'm not religious about it.
This is such a terrible analogy I'm left wondering if it's a joke.

Manual memory management is a pain in the ass that only still exists in modern software development because the alternatives are too slow for a huge class of applications. In other words, the state of the art hasn't caught up to some people's requirements, and so they decide that the pains of manually managing memory are worth it to achieve some specific benefit. How on Earth can you compare that to manually escaping user input? I mean, what do you gain, for the price of "almost certainly leaving your site open to an infinite array of vulnerabilities"? I'll just go ahead and answer that for you: none. None benefits. The state of the art is absolutely caught up to people's requirements, here. Manually escaping input makes no sense. It'd be like manually managing memory in some future time when we have zero-cost generational GC. The only excuse is masochism.

This is not a design issue.
This is absolutely a design issue.
 

sroylance

Ars Tribunus Militum
2,156
Don't let the framework fundamentalists get to you. Interleaving markup and code, and using inline variable interpolation (the ?= shortcut) can make for very readable and maintainable code as long as you're rigorous about writing well structured modular code.

Of the 3 you posted, the first is the easiest to read IMO. Using big echo statements to generate HTML in PHP is just adding a bunch of code and quoting that's unnecessary, and harder to read IMO. There's no benefit to the heredoc approach either.

My advice? Just go with the flow, and concentrate on writing good code that's clearly structured. What's 'right' is irrelevant to what's 'good'.
 

Blacken00100

Ars Legatus Legionis
10,130
Subscriptor
sroylance":34o82ssy said:
Interleaving markup and code, and using inline variable interpolation (the ?= shortcut) can make for very readable and maintainable code as long as you're rigorous about writing well structured modular code.
And if my ass farts diamonds, I'll be rich. It's about as likely as doing this well.
 

sroylance

Ars Tribunus Militum
2,156
Blacken00100":1j10nqvz said:
sroylance":1j10nqvz said:
Interleaving markup and code, and using inline variable interpolation (the ?= shortcut) can make for very readable and maintainable code as long as you're rigorous about writing well structured modular code.
And if my ass farts diamonds, I'll be rich. It's about as likely as doing this well.
Have you tried? It's an approach that worked fine for me, and I haven't gotten any complaints from the guy who took the project over. Odds are good you've used the result, actually.
 

Blacken00100

Ars Legatus Legionis
10,130
Subscriptor
Yes, actually, I have tried. And it works, in limited use cases, if you don't really give a shit about trivial things like security and failing safe. I learned from the mistake of doing it that way and didn't repeat the stupid.

As for "hurr, odds are good you've used this thing you hate!!!"--BFD? Everyone who hates Apache Velocity should avoid my employer, too. This shit is a meaningless statement.
 

sroylance

Ars Tribunus Militum
2,156
Blacken00100":36kf72fc said:
Yes, actually, I have tried. And it works, in limited use cases, if you don't really give a shit about trivial things like security and failing safe. I learned from the mistake of doing it that way and didn't repeat the stupid.

As for "hurr, odds are good you've used this thing you hate!!!"--BFD? Everyone who hates Apache Velocity should avoid my employer, too. This shit is a meaningless statement.
I find your unsubstantiated invective meaningless. I'll take working solutions over religion in any case.
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
I'll take working solutions over religion in any case.

When you don't define "correct" in the case of public facing web apps to include pretty rigorously avoid any and all chances of security issues* then I'd rather you didn't write any solutions at all thank you very much.

Using a language/APIwhere, to use it correctly you must add boilerplate to each and every usage of the API is just crazy.
You violate DRY for starters and that's one of the few close to universally true programming tenents.

* So making it easy to update in the face of changing threats would count too for example, something that would be in a similar category of "well it's a nice design idea but I wouldn't get all _religous_ about it"
 

Blacken00100

Ars Legatus Legionis
10,130
Subscriptor
sroylance":2ctoaucc said:
I find your unsubstantiated invective meaningless. I'll take working solutions over religion in any case.
Twig or equivalent is a working solution. You don't have to manually XSS-protect everything you splat on the page, you don't have to worry about nasty functions being in-scope where they shouldn't, you don't have to worry about some moron digging around in superglobals in places it makes no sense to do so...you know--basic shit that everyone should know and yet so few do.

Pogo-hopping christ. I mean, really? This should be obvious to you. Like with the other guy: do you think mysql_real_escape_string and string concatenation is a valid SQL strategy, too? It should be wonderful for you: both meaninglessly verbose and ineffective! :D


ShuggyCoUk":2ctoaucc said:
* So making it easy to update in the face of changing threats would count too for example, something that would be in a similar category of "well it's a nice design idea but I wouldn't get all _religous_ about it"
XSS is just a theory, man. Like gravity.
 
Status
Not open for further replies.