跳到主要內容

發表文章

手工 PHP CSRF 實作

現在自己寫程式都已經使用 CodeIgniter4 的 PHP 套件,系統會自動使用資安防護機制,如網路表單會使用 CSRF (cross-site request forgery)的機制保護,以防止不合法的資料輸入或程式機器人入侵。 但在之前未使用 CI4 之前的程式,如果要防止相關資訊安全漏洞的話,必要自己手工增加 CSRF 防護。 CSRF 產生: $_SESSION['token'] = md5(uniqid(mt_rand(), true)); 在表單添加 CSRF 隱藏欄位: <input type="hidden" name="token" value="<?php echo $_SESSION['token'] ?? '' ?>"> 在後續程序驗證 CSRF: $token = filter_input(INPUT_POST, 'token', FILTER_SANITIZE_STRING); if (!$token || $token !== $_SESSION['token']) {     // return 405 http status code     header($_SERVER['SERVER_PROTOCOL'] . ' 405 Method Not Allowed');     exit; } else {     // process the form }

Firebase 認證從 signInWithRedirect 改成 signInWithPopup

 之前有嚐試使用 Firebase 來製作網站的認證程序,最近有收到 Google 的來信,要把原本的認證程式從 signInWithRedirect() 改成 signInWithPopup()。 官方的說明 實作了一下,修改的方式非常簡單,就直接把認證程式改過來就行了。 因為是測試實作,所以先把原本的 signInWithRedirect() 先注解起來,加上 signInWithPopup()就行了。 實際測試運作下,可以正常認證。

CodeIgniter4 使用 Firebase 進行認證

  前置動作 完成 Firebase 專案設定,並新增好使用的應用程式(必須是網頁程式</>) 產生並下載保存服務帳號金鑰檔案(JSON) 備註:此金鑰JSON檔只能產生一次,若再次點選產生金鑰會產出另一份金鑰內容 安裝 在 CodeIgniter4 專案中安裝 firebase-php: composer require " kreait/firebase-php:^7.0 " 備註:firebase-php 7.0 僅支援 php 8,所以如果使用 php 其他版本就不要加上最後面的 :^7.0,composer 會自動使用 6.9 版 將服務帳號金鑰JSON檔案置於網站系統內,例如將json檔置於網站的/cert資料夾中 設定 在 .env 檔案中加入金鑰檔案的路徑 以 javascript 進行認證服務 登入頁製作 網頁加入相關資訊 PHP 認證處理 說明: 1. /auth/login 登入頁中,使用 Google 認證按鈕後,會進入 firebase 認證程序,認證完成後將轉址至 /auth/callback/?token=..... 2. 在 /auth/callback 程序中,認證使用者資料且取得權限後,存入 session 再轉至 /admin 管理頁 3. 在 firebase 使用者資料中,可以利用 customClaim 欄位來儲存額外的資訊。

PHP + CI4 的 PhpSpreadsheet 的處理

0、需要用到的 Library use \PhpOffice\PhpSpreadsheet\Spreadsheet; use \PhpOffice\PhpSpreadsheet\Writer\Xlsx; use \PhpOffice\PhpSpreadsheet\Reader\Xlsx; use \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; 參考資料: Welcome to PhpSpreadsheet's documentation - PhpSpreadsheet Documentation  1、上傳 excel 檔案並讀取內容 //處理上傳 try {             $thefile = $this->request->getFile('theuploadfile'); } catch (\Exception $e) { echo $e; exit; } try { $path = $thefile->store(); } catch (\Exception $e) { echo $e; exit; } //確認檔案格式並讀取內容 $filename = WRITEPATH . 'uploads/' . $path; $fileext = ucfirst($thefile->getClientExtension()); if (in_array($fileext, array('Xls', 'Xlsx'))) { $reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($fileext); $spreadsheet = $reader->load($filename); $sheet = $spreadsheet->getSheet(0); $data = $sheet->getCell('A1')->getValue(); } 2、產生 Excel 檔案 / /生成試算表並設定格式 $spreadsheet = new Spreadsheet(); $spreadsh...

幾個 Linux command

搜尋 n 天前有修改的檔案 find -mtime -n 刪除 n 天前建立的檔案 find -mtime +n -exec rm {} \; 以檔名規則搜尋檔案並刪除 find . -name "abc*" -delete 同步排除某些資料夾或檔案 rsync -r -q --exclude "dir*" --exclude "file*.txt" [source] [distination]

幾個網頁排版技巧

  1、首行縮排2字元 中文文章會習慣每段的首行縮排2字元 CSS設定:text-indent: 2em 2、利用 Web font loader 動態載入網路字型 JS: <script src="https://ajax.googleapis.com/ajax/libs/webfont/1.6.26/webfont.js"></script> <script>      WebFont.load({           google: {   families: ['Noto Sans TC','Droid Sans'] }   }); </script> 參考資料: 網路字型載入工具  |  Google Fonts  |  Google Developers typekit/webfontloader: Web Font Loader gives you added control when using linked fonts via @font-face. (github.com) 网页字体优化之字体异步加载 | Blog @ GIUEM Web字体载入利器(Web Font Loader)使用详解(Web字体加载缓慢解决办法)_web加载字体_lt_1111的博客-CSDN博客 Web字体载入利器-->Web Font Loader - w3ctech 3、常用CDN jQuery CDN Google Hosted Libraries  |  Google Developers 4、改善字型 render body { -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; font-smoothing: antialiased; }

Mysql 新增使用者

 單純新增使用者 create user 'username'@'localhost' identified by 'password'; 新增權限使用者 grant all privileges on *.* to 'username'@'localhost' identified by 'password' with grant option;