[JavaScript]オプショナルチェーン備忘録

1

const tmp = {name:"abc"};
console.log(tmp.address?.length);
undefined

 
 

2

const tmp = {name:"abc"};
console.log(tmp.address?.length+tmp.city?.length);
NaN

 
 

3

const tmp = {name:"abc"};
console.log(tmp.address?.length+tmp.city?.length > 4);
false

 
 

4

const tmp = {name:"abc"};
console.log(tmp.address?.length+tmp.city?.length == 0);
false

 
 

5

const tmp = {name:""};
console.log(tmp.name?.length+tmp.city?.length <= 1);
false

 
 

6

const tmp = {name:""};
console.log(tmp.name?.length <= 1);
true

 
 

[PHP]CORS

public static function set_header_for_cors($domain = '*')
{
    header("Access-Control-Allow-Origin: ".$domain);
    header("Access-Control-Allow-Methods: GET, POST");
    header("Access-Control-Allow-Headers: Accept, Content-Disposition, Content-Type, Content-Length, Accept-Encoding");
}

[MySQL]AES 256 CBC

ivはDBに保存しているが16進数文字列でファイルに記述してもよい

SET block_encryption_mode = 'aes-256-cbc'
SET @key_str = SHA2('XXXXXXX',512)
SET @init_vector = RANDOM_BYTES(16) ※INSERTの時だけ実行
INSERT INTO `table_name`(shop,token,iv,created) VALUES(HEX(AES_ENCRYPT('ABC', @key_str,@init_vector)),HEX(AES_ENCRYPT('DEF', @key_str,@init_vector)),HEX(@init_vector),'2024-05-01 11:12:13')
UPDATE `table_name` SET shop = HEX(AES_ENCRYPT(:shop, @key_str,@init_vector)),token = HEX(AES_ENCRYPT(:token, @key_str,@init_vector)),iv = HEX(@init_vector),modified = '2024-05-01 11:12:13' WHERE id = 5
SELECT convert(AES_DECRYPT(UNHEX(shop), @key_str,UNHEX(iv)) USING utf8) AS shop,convert(AES_DECRYPT(UNHEX(token), @key_str,UNHEX(iv)) USING utf8) AS token,created FROM `table_name` WHERE convert(AES_DECRYPT(UNHEX(shop), @key_str,UNHEX(iv)) USING utf8) = 'ABC'

[Laravel]ログイン後のURLを変える

下記ファイルの
app\Http\Controllers\Auth\AuthenticatedSessionController.php
 

return redirect()->intended(route('dashboard', absolute: false));

 

return redirect()->intended(route('post.index', absolute: false));

[WordPress]不要ページを無効化

/**
 * WordPress標準の不要ページを無効化
 * @param WP_Query $query
 */
function force_404($query)
{
    if(is_attachment() || is_author() || is_search() || is_date() || is_tag()){
        /** 404ページを返す */
        $query->set_404();
        /** 404コードを返す */
        status_header(404);
        /** キャッシュの無効化 */
        nocache_headers();
	}
}
add_filter('parse_query', 'force_404');

[Laravel]コンポーネント作成

下記作成
resources\views\components\message.blade.php
 
内容

@props(['message'])
@if($message)
<div class="p-4 m-2 rounded bg-green-100">{{$message}}</div>
@endif

 
bladeファイルの表示したい場所に下記追記

<x-message :message="session('message')" />

 
コントローラーに下記を追記する

$request->session()->flash('message','あいうえお');

 
下記コマンドでコンポーネント作成する方法もある

sail artisan make:component Message

[JavaScript]ブラウザバック判定

isBrowserBack(){
    const perfEntries = performance.getEntriesByType("navigation");
    let result = false;
    perfEntries.forEach((perfEntry) => {
        if(perfEntry.type == 'back_forward'){
            result = true;
        }
    });
    return result;
}