When PHP code should really be treated as unsafe

Yesterday I took a part in interview for PHP developer position. My interview task was to solve fifteen questions in quite simple test. One of the questions was to decide if given code sample be treated as unsafe and in which conditions.

I gave a wrong (as it turned out) answer and the argumentation from the intervieerw was quite surprising for me in the first time. Finally I realized my mistake.

Read More “When PHP code should really be treated as unsafe”

Alternative module configuration that does not affect main configuration

In Yii main application is actually a module (core one) so each module configuration actually shares nearly everything what you can put to main application’s configuration file.

Thus, you can configure any Yii module, just as you would do with your main application. The only difference is that you don’t use external configuration file, but CModule::configure() function instead.

Read More “Alternative module configuration that does not affect main configuration”

Passing current or last page full URL as a part of URL

By using five separate PHP functions, in correct order, you can easily pass (code and decode) entire page’s URL as a parameter of any other script call, redirect etc. And be sure that it will be read (decoded) correctly, no matter, how long your URL is or what kind of characters it is using.

You can encode any URL (current page, last visited page or any otherwise important) and pass it in another URL in the way that your user won’t notice that you’re actually passing an URL.

Presented solution can potentially be used as URL shortening service. Generated URLs aren’t that short (as maybe expected), but for really long URLs it does provides some shortening.

Read More “Passing current or last page full URL as a part of URL”

CMarkdown usage examples

You can use CMarkdown class as typical Yii widget:

[code language=”php”]
<?php $this->beginWidget(‘CMarkdown’); ?>
_Markdown_ *example*
<?php $this->endWidget(); ?>
[/code]

or directly as a function:

[code language=”php”]
$text = ‘_Markdown_ *example*’;
$md = new CMarkdown;
echo $md->transform($text);
[/code]

That’s pretty much everything, folks!

Convert CActiveDataProvider to associative array

Here is an example on how to convert CActiveDataProvider to a simple associative array.

[code language=”php”]
$dataProvider = new CActiveDataProvider(‘Users’);
$data = array();

foreach($dataProvider->getData() as $r)
{
$rowArray = array();

foreach($dataProvider->model->tableSchema->columns as $c) $rowArray[$c->name] = $r[$c->name];

$data[] = $rowArray;
}
[/code]

Columns names are read from table schema, so you don’t have to know table (model) structure to use this.

Missing colon in field’s labels

By default, Yii renders all forms with field’s labels not containing colon after label. This may be unwanted effect for some. The easiest way would be to change field’s label to contain that missing colon. But, that would produce an even more unwanted side-effect of having that colon included (as part of field label) in error message, when validation fails. Here is my simple workaround for this problem.

Read More “Missing colon in field’s labels”

Correcting code completion in NetBeans

General code completion in NetBeans works quite good (expect for its speed), but since NetBeans was made for Java and has only “added” support for PHP then it has some issues code completion for PHP and for coding in Yii framework it fails nearly completely.

Here you’ll find approach to fix this and make Yii developers using Netbeans life a little bit easier.

Read More “Correcting code completion in NetBeans”