Saturday, January 27, 2024
Real time chat
Tuesday, January 23, 2024
The Turkish uppercase "İ"
PHP's mb_strtoupper function converts its input string to uppercase. For Turkish characters, "İ" is a special case, the other characters (ŞÇÖĞÜ) work fine with UTF-8 encoding. mb_strtoupper("izmir", 'UTF-8') returns "IZMIR". If you want it to return "İZMİR" you have to have Turkish locale installed on your OS, add setlocale(LC_ALL, 'tr_TR.UTF-8') to your startup index.php and use mb_strtoupper("izmir", 'tr_TR'). If you don't have Turkish locale on your OS, mb_strtoupper("izmir", 'tr_TR') will return false. In your index.php, check if locale is set correctly by issuing echo setlocale(LC_ALL, 0); If the printout is "LC_COLLATE=C;LC_CTYPE=English_United States.1252;..." you were not able to set the locale to Turkish. I have a search function that compares two strings. I don't have Turkish locale on my development environment, therefore I use the following hack (PHP 7.4):
$modProduct = str_replace("İ", "i", $productName); //Change "İ" to "i" so that for UTF-8, it's
uppercase becomes "I" instead of "İ"
$modSearch = str_replace("İ", "i", $searchTerm);
if (strpos(mb_strtoupper($modProduct,'UTF-8'), mb_strtoupper($modSearch, 'UTF-8'))!==false) {...}
Friday, January 19, 2024
PHPStorm structural search
if (isset(...)) { ... } else { ... }you can use PHPStorm structural search (Edit - Find - Search Structurally):
if (isset($a$)) { $b$ } else { $c$ }
Monday, January 15, 2024
Use SQL COUNT(*) instead of PHP count()
count($db->query("SELECT * FROM customer_comments WHERE status = 1")->rows)use
$db->query("SELECT COUNT(*) FROM customer_comments WHERE status = 1")->valueUsing COUNT(*) is faster because databases are optimized for such operations. When you fetch all rows into your application and then count them using count(), you're moving the data processing to the application level, which is generally less efficient.
Friday, January 12, 2024
Software complexity
- User writes comment
- User presses submit button
- A message saying "comment will be visible after admin approval" is shown to user
- Comment is saved to database for admin approval
- Admin reviews and approves comment
- When product page is loaded, approved comments are shown
- If the user is not logged in, they must login. If the user has no account, they must create an account. After login / account creation, they should be redirected back to same product page with "comment will be visible after admin approval" message. Do not show that message for normal product page loads.
- After pressing submit button, user might want to edit or delete the comment.
- Although admin approval prevents spam, a malicious user could still:
- Send large text, overloading the database
- Send a large number of comments in a short time, overloading the server (DoS)
- Do SQL injection
- Do Cross-Site Scripting (XSS)
Thursday, January 11, 2024
Button click not working
<button style="padding-left: 20px; padding-right: 20px;
position: relative; z-index: 99;" type="submit">Yorum yap</button>
Wednesday, January 3, 2024
Using both static and dynamic versions of the same class
class Request {
public static $get = array();
private function __construct() {
// Set the instance properties so that they can be accessed dynamically
// until I convert every instance of request property use to static, e.g. Request::$get
$this->get = &self::$get;
}
public static function create() {
//Sanitize input data, primarily to prevent issues like cross-site scripting (XSS)
$_GET = self::clean($_GET);
self::$get = $_GET;
return new self();
}
...