How to write secure Yii1 applications

Comparing #11 with #13

Revision #13 was created by Boaz Boaz on Nov 19, 2012, 2:24:57 PM.

utilizing the recommendation to cast to int suggested above...

Content

[...]
<?php
$messages = array("Rock'n roll", 'Say "hello"');
$title = "D'accord";
Yii::app()->clientScript->registerScript('snippet', "
function displayMsg() {
var messages = <?php echo
CJavaScript::encode($messages); ?>; var title = '<?php echo CJavaScript::quote($title); ?>';
// ...
}
[...]
<?php
// still lacks validation, but more secure
MyModel::model()->findByPk(
(int)$_GET['id'])->delete(); $comments = Comment::model->findAllByAttributes(array('user_id' => (int)$_GET['id']);
```

This is a general principle: if you build your SQL condition in pure text, you take more risks than a more PHP approach.
For most DB functions, **prefer array parameters to string parameters**.
Here is another example using PHP arrays:
[...]