發表文章

目前顯示的是有「php」標籤的文章

PHP彷JWT打包產生需要的資料,把回傳回來的資料驗證解碼

執行: http://sandbox.onlinephpfunctions.com/code/8349c158d68cdf644dd07d802f3782384fb4e7b7 使用範例: 加密的密碼 $salt = '11223344'; // 需要傳遞的陣列 $codevalue_array = array(   'Amt' => '111',   'MerchantOrderNo' => 'ertgyhujioiuytre' ); // 打包產生需要的資料 $send_code = jwtenc($salt,$codevalue_array); var_dump($send_code); // 把回傳回來的資料驗證解碼 $codevalue = jwtdec($salt,$send_code); var_dump($codevalue);

關於PHP, HTML and Javascript 程式碼的混肴與壓縮

圖片
把 php 程式碼的混肴密, 可以加上密碼 Free Online PHP Obfuscator http://www.fopo.com.ar/ 把 php 加密 jjencode demo http://utf-8.jp/public/jjencode.html 美化你的 html code Format, Beautify, Maxify, Unpack or Deobfuscate JavaScript/jQuery/HTML/JSON/CSS Codes http://jenson.in/codeformatter/ 壓縮你的 html code Speed up your web pages https://htmlcompressor.com 壓縮 html # 原始檔案內容 [mtchang@jangmt]$ cat phpobs.php echo sha1('12345678'); ?> # 加密過後的內容-執行結果 [mtchang@jangmt]$ php phpobs_code.php 7c222fb2927d828af22f592134e8932480637c0d # 沒有加密-執行結果 [mtchang@jangmt]$ php phpobs.php 7c222fb2927d828af22f592134e8932480637c0d # 檔案大小差很多 [mtchang@jangmt]$ ls -la phpobs* -rw-rw-r--. 1 mtchang mtchang 6634 Apr  3 01:17 phpobs_code.php -rw-rw-r--. 1 mtchang mtchang   32 Apr  3 01:16 phpobs.php 原始碼貼上去 立即混淆看不懂程式碼

PHP 檢查傳入的 DATE 日期是否合法,不合法的話就以現在的日期為值。

PHP 檢查傳入的日期時間是否合法,不合法的話就以現在的日期為值。   // get example: ?current_datepicker=2017-02-03   // ref: http://php.net/manual/en/function.checkdate.php   function validateDate($date, $format = 'Y-m-d H:i:s')   {       $d = DateTime::createFromFormat($format, $date);       return $d && $d->format($format) == $date;   }   // 取得 get 傳來的變數,如果有的話就是就是指定的 yy-mm-dd 沒有的話就是今天的日期   if(isset($_GET['current_datepicker'])) {     // 判斷格式資料是否正確     if(validateDate($_GET['current_datepicker'], 'Y-m-d')) {       $current_datepicker = $_GET['current_datepicker'];     }else{       $current_datepicker = date('Y-m-d');     }   }else{     // php 格式的 2017-02-24     $current_datepicker = date('Y-m-d');   }   // var_dump($current_datepicker);

PHP session_write_close()

圖片
PHP函式 php session_write_close() End the current session and store session data. session 的資料需要在 script 結束時才會儲存,所以當有2 個 script 同時執行時,後面的 script 會等前面的 script 執行完成再來跑,如果設定了  session_write_close() 就可以立即告訴程式,我後面的程序不會寫入 session 了,其他程式可以讀取 session 的內容來處理。 好處就是:程序不會被前一個 session 卡住,但是寫入 session 的行為就要很小心的處理。 範例: http://konrness.com/php5/how-to-prevent-blocking-php-requests/ 說明:  http://php.net/manual/en/function.session-write-close.php Session data is usually stored after your script terminated without the need to call session_write_close(), but as session data is locked to prevent concurrent writes only one script may operate on a session at any time. When using framesets together with sessions you will experience the frames loading one by one due to this locking. You can reduce the time needed to load all the frames by ending the session as soon as all changes to session variables are done. ref: http://xyz.cinc.biz/2012/01/php-session-lock.html 

RabbitMQ 實作 AMQP 的開放原始碼訊息代理軟體

圖片
RabbitMQ 是一個訊息中介軟體 (broker) 可以接受並轉發訊息,他是 AMQP 的實作。 功能類似郵局機制,類似 unix Email system。 主要處理的存儲、轉發 binary blobs of data (二進制資料) 那 AMQP 它和傳統的 SMTP 差異在哪裡??  重點在可以 非同步,並且可靠 。 ref:  http://blogs.mulesoft.com/dev/news-dev/amqp-and-the-future-of-web-messaging/   系統中的角色: 訊息發布者 publisher = 訊息消費者 consumer 訊息代理 brokers 訊息生產者 producers 訊息交換模型 (ref:  http://rabbitmq.mr-ping.com/AMQP/AMQP_0-9-1_Model_Explained.html ) 使用 RabbitMQ 的顧客  https://blog.pivotal.io/channels/data-science-pivotal Pivotal Labs 原來不只玩 big data 它是一間很有趣的公司呀!!( MADlib 是它開發中的產品..) 因為還不是很熟悉,所以放了很多參考連結: 官方網站 http://www.rabbitmq.com    in github  https://github.com/rabbitmq 官方教學  http://www.rabbitmq.com/getstarted.html  中文教學  http://rabbitmq.mr-ping.com/ WIKI 介紹 https://zh.wikipedia.org/wiki/RabbitMQ PHP 函式 http://php.net/manual/fa/book.amqp.php  PHP amqplib in GITHUB  https://github.com/php-amqplib/php-amqplib  Python 函式 https:/...

Redis-Server with PHP 的使用 in CentOS7

圖片
Redis-Server with PHP 的使用 in CentOS7 * Redis DB server 的用途 Redis 是一個 key-value 架構的 database , 且因為它工作在記憶體上面,所以速度非常的快。 可以用來當 DB cache 加速的功能或是需要快速反應的 DB * CentOS 7 的 Redis-server 安裝方式 # yum install redis -y  * 安裝參考 http://sharadchhetri.com/2014/10/04/install-redis-server-centos-7-rhel-7/ * 通常會搭配 php 等 client 端工具一起安裝, 這裡我是以 php56w 的版本安裝, 因為有比較多 lib 支援 # yum install php56w-pecl-redis * php56w 可以參考 webtatic 釋出的 yum repo 安裝參考 https://webtatic.com/packages/php56/ RHEL7 OR EPEL7  官方網站跑的版本有點慢,可以先參考這個版本。 * 啟動 # systemctl start redis.service * 預設開機啟動 # systemctl enable redis.service * 看狀態 # systemctl status redis.service * 測試 , 剛裝好的 redis-server 是沒有密碼的, 且只能 127.0.0.1 執行。 可以用 redis-cli client 端工具測試 PONG 是正確的回應。 [root@c7 ~]# redis-cli ping PONG * 所以我都會設定個密碼, 並且把 bind 打開讓所有人都可以連上來。 * 請找到下面兩行, 修改為你要的密碼及主機 ip # vim /etc/redis.conf bind 111.111.22.22 requirepass 123456 * 重新啟動 # systemctl restart redis.service * 驗證看看 port , 應該是 6379 # netstat -antlp | grep ...

將 CSV 檔案加上 UTF BOM 讓它可以在 Windows 被 EXCEL 正確的開啟沒有亂碼的問題

圖片
將 CSV 檔案加上 UTF BOM 讓它可以在 Windows 被 EXCEL 正確的開啟沒有亂碼的問題 在 UTF-8 文件中放置 BOM 主要是微軟系統的習慣。但是真的沒有必要帶拉!!!! http://stackoverflow.com/questions/2223882/whats-different-between-utf-8-and-utf-8-without-bom According to the Unicode standard, the BOM for UTF-8 files is not recommended: 圖片和內文不太相關 下面提供一種在 windows 上面,政治正確的解法。 XDXD // ======================================================== // 產生現在的秒數 $time_string = date("YmdHis"); // 寫入 utf8 with BOM function writeUTF8File($filename,$content) {     $f = fopen($filename, 'w');     fwrite($f, pack("CCC", 0xef,0xbb,0xbf));     fwrite($f,$content);     fclose($f); } $f = 'uploads/private/'.$time_string.'.csv'; $fdir = 'uploads/private/'; // 檔案不存在就先建立檔案 if (!file_exists($fdir)) {     mkdir($fdir); } writeUTF8File($f,$csv); echo 'CSV 輸出完成,請點選 連結 下載。 '; // ========================================================

取得 mysql 資料表中的「欄位名稱」以及「欄位註解」

圖片
取得 mysql 資料表中的「欄位名稱」以及「欄位註解」 在 MySQL 資料庫中,宣告資料表的過程,可以一併把欄位名稱寫在 COMMENT 選項中,如果在程式可以把這個 COMMENT 也抓出來配合欄位的名稱,可以產生自動把整個表 DUMP 出來成為一個 CSV 不就很方便。 圖片和內文不太相關 // ----------------------------------------------- $table='表格名稱'; // 資料庫中表的欄位抬頭資訊 $col_sql = "SELECT COLUMN_NAME,COLUMN_COMMENT FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '$table';"; $col = runSQL($col_sql); // var_dump($col); // 資料欄位 $sql = "SELECT * FROM `$table` WHERE 1;"; $row = runSQL($sql); // var_dump($row); $csv = ''; // 欄位名註解 //echo " "; for($i=1;$i<$col[0];$i++){ //echo $col[$i]->COLUMN_COMMENT.", ";  $csv = $csv.$col[$i]->COLUMN_COMMENT.", "; } $csv = $csv."\n"; // 欄位名稱 //echo " "; for($i=1;$i<$col[0];$i++){ //echo $col[$i]->COLUMN_NAME.", ";  $csv = $csv.$col[$i]->COLUMN_NAME.", "; } $csv = $csv."\n"; // 資料列取得欄位 //echo " ...

mysql 中文亂碼的避免及 php mysql pdo 宣告取用方式

圖片
Mariadb 和 Mysql 都會有亂碼, 只要它預設編碼還是在 latin 的時還是一直會發生. 所以改成 utf8 編碼是一個對於東亞語系的文字,會比較方便。 UTF8 的介紹請看 wiki https://zh.wikipedia.org/wiki/UTF-8 xxx_bin (區分大小寫) xxx_general_ci (不區分大小寫) Collation 每個字符集的排序規則 http://dev.mysql.com/doc/refman/5.0/en/charset-unicode-sets.html 在 mysql 資料庫中有兩個編碼系統 UCS2 及 UTF8 utf8 使用 1~3 bytes ,帶有 65536 個組合的編碼 utf32 與 utf8mb4 是完整的的 unicode ,差異在 utf32 固定 4 bytes ,而 utf8mb4 則是彈性使用 1~4 bytes 。 utf8mb4 從 MySQL 5.5.3 (released March 2010). 開始提供 目前預設的編碼系統為 xxx_general_ci (default) 為了中文語系使用不要出問題,建議修正如下: ----------------------------------------------------- # 修正 mysql 的設定檔 [root@c7 my.cnf.d]# more /etc/my.cnf.d/my-huge.cnf  [client] default-character-set=utf8mb4 [code] default-character-set=utf8mb4 [mysqld] character-set-server=utf8mb4 collation-server=utf8mb4_general_ci # restart mysql service # 檢查系統的編碼變數 ---------------------------- [root@c7 my.cnf.d]# mysql -u root -p Enter password: MariaDB [(none)]> SHOW VARIABLES WHERE Variab...

php7 vs. GO lang vs. C on CentOS 7 Linux 速度比較

圖片
php7 vs. GO lang vs. C on CentOS 7 Linux 速度比較 繼上一篇後,想說 php7 已經很快了,但是到底和原生 C 差多少..... 所以就抓了php7 , GO 及 C 三種語言來比較看看氣泡演算法, 12 各項目排序 90萬次花費的時間。 結論: PHP7 ==>  5.9242088794708 s GO  ==>  0.228.885223 s C ==> 0.174889 s 換算一下, ms, us 的單位, 結論是 php7 連 GO 和 C 的車尾燈都看不到... GO 還可以看得到C 的車尾燈!!!!! 且 GO 比 C 好寫多了,我可以接受這一點點的微小差距。 有圖有真相.... C bubble sort 演算法  http://www.programmingsimplified.com/c/source-code/c-program-bubble-sort C 計算時間差 http://www.binarytides.com/get-time-difference-in-microtime-in-c/ php 及 go 的程式碼 https://jaxbot.me/articles/benchmarks_nodejs_vs_go_vs_php_3_14_2013 別人做的 php vs go https://www.ransona.com/blog/2014/11/06/go-vs-php-performance-restful-service/

升級 php7 可以不用急著換電腦.....!!!! on CentOS 7

圖片
升級 php7 可以不用急著換電腦.....!!!! on CentOS 7 Linux 先講結論: 升級前  2.767 sec 升級後  0.88 sec ---------------------- 速度增加了 3.14 倍 ,這台是 Intel Core I5 CPU, RAM 4GB 的個人PC。 更新可以參考 這篇文章 , CentOS 6 OR CentOS 7 都可以更新。 https://webtatic.com/packages/php70/  但是要注意,如果你的 Linux 系統已經有安裝第三方套件,或手動安裝的請小心更新後會毀掉。我的是都用官方的套件及 EPEL更新,直接上去沒有發生大的問題資料也都存在。 另外如果你有自行開發的程式,也需要注意套件的相容性,再行安裝。 測試程式,我是抓   http://www.php-benchmark-script.com/ 這個測試程式。 程式沒有針對平行化、I/O 這些評估,但已經明顯看得出差異。

PHP Multi Process 程式以 PING 多主機應用為範例

嘗試使用 php pcntl_fork 寫各 ping 程式,一次偵測多台主機。 但目前還有些問題,如子程序沒辦法再程式中傳回父程序,這部份還要再仔細看一下手冊看看該如何處理。 https://github.com/mtchang/ping/blob/master/ping.php ref: http://php.net/manual/en/function.pcntl-fork.php http://pkwbim-programming-note.blogspot.tw/2008/01/phpmulti-process-1.html http://www.systn.com/data/articles/207_tw.html

WAMPServer 2.5 php_pgsql.dll 有問題?無法正常的工作。

圖片
WAMPServer 2.5 php_pgsql.dll 有問題?無法正常的工作。 因為想要讓開發的工作更有效率,於是在自己的電腦上安裝 WAMP 當成本地端的開發工具 WAMP 預設的 DEBUG 工具很齊全,對於除錯真的很方便。 但今天改開發 PHP + PostgreSQL 的程式,發現明明 PHP_PDO_PGSQL 都有啟動,但是 phpinfo() 怎樣都看不到相關的模組訊息。(應該要有 pdo_pgsql and pdo_pgsql 這兩個模組才是) 查了半天,發現在 PHP for windows 5.25 "php_pgsql.dll" 還可以工作,升級 PHP 到 5.5.12 就不正常了.... 快速的修正方式: (1) 安裝 Postgresql for windows 版,在本地端機器上面。 http://www.enterprisedb.com/products-services-training/pgdownload#windows 裝好後,看看在地下的路徑是否有這個檔案 libpq.dll (2) 將該檔案讓 apache 掛載 打開 windows apache httpd.conf 設定檔,找個好地方把底下這一行放到設定檔案內,然後重新啟動 APACHE LoadFile "C:/Program Files/PostgreSQL/9.3/bin/libpq.dll" (3) 去看看你的 http://localhost/?phpinfo 應該就有 pdo_pgsql and pdo_pgsql 這兩個模組了。 pdo_pgsql and pdo_pgsql 這兩個模組 參考: http://stackoverflow.com/questions/551734/php-not-loading-php-pgsql-dll-on-windows

AppServ 懶人包的資安問題與檢測(請不要再使用 APPSERV 懶人包)

圖片
此文同時刊載於 http://www.openfoundry.org/tw/foss-programs/9073-appserv- ---- APPSERV ( http://www.appservnetwork.com/ )是一個 Windows 下很成功的懶人包專案,它把 Apache 、PHP、MySQL以及 PHPMyAdmin 整合打包成為一個安裝檔案,對於入門的程式開發人員很方便。也因為它在 2006 年就推出所以到現在仍是下載率很高的專案,每個月約有 10 萬次下載。 圖1:APPSERV每月下載次數 但是問題就在這裡,它目前的版本為 2008-06-10 的版本( http://sourceforge.net/projects/appserv/files/?source=navbar ),作者已經有 5 年多沒有更新了,但是還是有那麼多人下載使用。 在 2009 、2010 有很多 phpMyAdmin 的漏洞陸續被公開,以 CVE-2009-1151( http://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2009-1151 ) 為例,它說明在 phpMyAdmin 的 setup.php 程式中有個程式碼可以寫入檔案的漏洞存在。如果找一下還會發現已經有人將它寫成自動化入侵的工具程式。 圖2: CVE-2009-1151自動化入侵工具 如果知道漏洞存在並補強其實不會造成什麼問題,但問題在於使用 APPSERV 的大多為初學者或對於系統程式不熟悉的使用者,只求網站能工作不會注意這些細節的問題。如果你用 google 的 inurl + site + intext 搭配搜尋你會發現真的很多人不在意,且甚至把這個網站它放到公開的網路上工作。關鍵字:「phpmyadmin  scripts  setup.php win32」 圖3: google「inurl:phpmyadmin/scripts/ site:edu.tw」 圖4: 沒有保護的 scripts 目錄,透過 google 就可找到 在 2013/05 月的時候 TACERT 已經針對這部份的入侵細節做了一份攻擊行為的分析報告( http://cer...

OCI for PHP 安裝 in CENTOS6 x86_64

ORACLE 的 php 支援說明 http://www.oracle.com/technetwork/topics/php/downloads/index.html 需要先安裝 Oracle Database Instant Client http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html 下載,他會依據作業系統給你載點,並且要求你登入 oracle (免費註冊) http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html 請抓取  basic 及 devel 這兩個檔案 [root@power app]# rpm -ivh oracle-instantclient11.2-basic-11.2.0.3.0-1.x86_64.rpm  [root@power app]# rpm -ivh oracle-instantclient11.2-devel-11.2.0.3.0-1.x86_64.rpm   # 安裝 php-devel 、php-pear 以及 gcc 編譯套件 [root@power app]# yum install php-devel php-pear gcc # 使用 pecl 直接安裝  http://pecl.php.net/package/oci8  ,這是一個 PHP 的元件,會需要編譯所以需要上面兩個 oracle instat client 套件的資料 [root@power app]# pecl install oci8 WARNING: channel "pecl.php.net" has updated its protocols, use "pecl channel-update pecl.php.net" to update downloading oci8-2.0.6.tgz ... Starting to download oci8-2.0.6.tgz (189,668 byte...

Doku WIKI 安裝(CENTOS 6 with SELinux)

圖片
Doku WIKI安裝 Doku wiki 是一套免用資料庫的 wiki 套件,他需要的程式語言為 PHP 安裝、使用、備份都很方便。底下案例以 Doku wiki 的安裝在 student 的個人網頁目錄內為範例說明,此範例是在 SELinux 打開的狀況下安裝,過程中會遇到 SELinux 的問題並且排除該問題。 此範例  http://person250.example.com  虛擬主機已經設定對應  http://person250.example.com/~student/  個人目錄(/home/student/public_html) 並且整個路徑目錄為 755 ,並開啟 SELinux 的 setsebool -P httpd_enable_homedirs=1 讓個人網頁目錄可以正常工作。 DokuWIKI官網: https://www.dokuwiki.org/dokuwiki  免資料庫的 wiki 程式, 使用 student 身份。 # 請上官方網站抓下最新的檔案,必且放到該目錄底下,請確定已經設定好個人的網頁目錄了。 [student@demo public_html]$ pwd /home/student/public_html [student@demo public_html]$ wget http://www.splitbrain.org/_media/projects/dokuwiki/dokuwiki-2012-01-25b.tgz [student@demo public_html]$ tar zxvf dokuwiki-2012-01-25b [student@demo public_html]$ mv dokuwiki-2012-01-25b wiki 觀看duki wiki  http://person250.example.com/wiki  網頁,點選 run install並依據提示修正目錄權限 student 身份 [student@demo public_html]$ chmod 777 wiki/conf/ 再次觀看duki wiki  http://person250.example.co...

php list 的 url cache 及 footer 問題

php list 中可以插入遠端的 URL 當作郵件內容,但是內容只要輸入後會被 cache 在 mysql 資料庫中 資料表示 url_cache 如果想立即生效可以從 sql 中刪除被 cache 的 web url 文件 ,但是 url cache refetch time 是可以設定的,預設是 3600 秒,只要修改 phplist 目錄中 config/config.php 檔案中的這一段就可以達成 # send a webpage. You can send the contents of a webpage, by adding # [URL:http://website/file.html] as the content of a message. This can also be personalised # for users by using eg # [URL:http://website/file.html?email=[email]] # the timeout for refetching a URL can be defined here. When the last time a URL has been # fetched exceeds this time, the URL will be refetched. This is in seconds, 3600 is an hour # this only affects sending within the same "process queue". If a new process queue is started # the URL will be fetched the first time anyway. Therefore this is only useful is processing # your queue takes longer than the time identified here. define('REMOTE_URL_REFETCH_TIMEOUT',300); 另外在系統中,會自動增加 phplist 的廣告及 footer 訂閱的連結,如果想拿掉可以 修改 admin/send...

PHPLIST 使用及設定 - 發信設定

phplist 是一套功能很強大的發信工具,但他的製報功能不是很強..... http://www.phplist.com/ 這是他的特色(官方網站說明) phplist is a one-way email announcement delivery system . It is great for newsletters, publicity lists, notifications, and many other uses. (It is different from group mailing list systems like mailman .) The Web Interface lets you write and send messages, and manage phplist over the internet. phplist keeps sending messages from your web server, even after you shut down your computer. 100 000 + subscribers . phplist is designed to manage mailing lists with hundreds of thousands of subscribers. phplist is excellent with smaller lists too! No duplicate messages. No 'forgotten' messages. phplist manages message delivery with a message queue, ensuring that every subscriber gets the email message, and that no subscribers receive two copies, even if they're subscribed to more than one list! Open/View Tracking tells you how many users opened your email message. This provides a minimum...