Last active
November 29, 2018 18:44
-
-
Save tolawho/4f2ebe1a9b6a40bade56a624cb29a025 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$status = ''; | |
/** | |
* 1. Đoạn này sai cơ bản, check isset cho filter nhưng lại lấy giá trị cbb, | |
* nếu có filter mà ko có cbb là có lỗi(có thể viết nhầm cbb thành 1 tên khác). 2 điểm về chỗ đúng rồi | |
* 2. Chưa filter giá trị $status để chống sql injection: ví dụ dùng addslash để khử dấu ' trong cbb, hoặc quy định biến là kiểu số, ... | |
* | |
*/ | |
if(isset($_GET('filter'))) { | |
$status = $_GET('cbb'); | |
} | |
/** | |
Hạn chế dùng biểu thức <?= ... ?>, tùy vào môi trường người cài đặt mà short tag có được bật không, nếu ko bật là oẳng | |
Thay vào đó <?php echo ... ?> | |
... | |
... | |
*/ | |
if($status == "") { | |
$que = "select * from banh"; | |
} else { | |
$que = "select * from banh where maLoaiBanh = '" . $status . "'"; | |
} | |
// Đoạn trên có thể viết như này cho code sáng, hạn chế block if else | |
// Tham khảo https://phpmd.org/rules/cleancode.html#elseexpression | |
$que = "select * from banh"; | |
$que .= $status ? " where maLoaiBanh = '" . $status . "'" : ''; | |
// hoặc | |
$que = "select * from banh"; | |
if($status) { | |
$que .= " where maLoaiBanh = '" . $status . "'"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment